A story from Srimad-Bhagavatam.
There was once a young brahmana named Ajamila, who was a reservoir of good character and conduct. He was mild and gentle, always truthful, well versed in the Vedic scriptures, and very pure.
One day, on the request of his father, Ajamila went to the forest to collect fruit, flowers and grasses. On the way home, he came upon a very lusty low-class man embracing and kissing a prostitute. Both of them were drunk, and the man was smiling, singing and enjoying himself without shame. The prostitute's eyes were rolling in intoxication, and her dress had become loose. When Ajamila saw her, the lusty desires in his heart awakened, and in illusion he fell under their control.
Remembering the instructions of the scriptures, Ajamila tried not to gaze at the woman. He strained to control his lusty desires with the help of his knowledge and intellect, but he failed and as a result lost all his good sense. In the days that followed he always thought about the prostitute, and within a short time he took her as a servant in his house and abandoned all the brahminical principles.
Ajamila began spending the money he had inherited from his father on various gifts to satisfy the prostitute. He even abandoned his beautiful young wife, who came from a very respectable family. Thus Ajamila spent his long life irresponsibly breaking all the rules and regulations of the holy scriptures. He was unclean and addicted to forbidden activities. Living extravagantly, he maintained his family by harassing others, cheating, gambling and theft.
Over the eighty-eight years of Ajamila's life, he fathered ten sons by the prostitute. Ajamila's youngest son, a baby named Narayana, was naturally very dear to both his father and mother. The child's broken language and awkward movements greatly endeared him to old Ajamila, who always took care of him and enjoyed his activities.
Narayana is a name for God, and thus unintentionally the old man was always chanting the holy name of the Lord. Although he was referring to his son and not the original Narayana, Ajamila was becoming purified because the holy name of Narayana is very powerful. Always taking care of his son and calling his name, Ajamila was not aware that his own life was now exhausted. As the time of death approached for the foolish Ajamila, he began thinking exclusively of his son Narayana.
At the moment of death, Ajamila saw three awkward persons with deformed bodily features, fierce and twisted faces, and hair standing erect on their bodies. They were the Yamadutas (the servants of Yamaraja, the lord of death), and they had come to take Ajamila away to their master's abode. When Ajamila saw them, he became extremely bewildered, and because of his attachment to his child, who was playing a short distance away, he loudly and tearfully called the child's name: "Narayana!"
Immediately the Visnudutas, the servants of the Supreme Lord Visnu (Narayana), arrived on the spot. They had heard the holy name of their master from the mouth of the dying Ajamila, who had chanted without offense because he had chanted in complete anxiety. As the Visnudutas arrived, the Yamadutas were snatching the soul from the body of the dying Ajamila. With resounding voices, the messengers of Lord Visnu ordered them to stop and thus saved Ajamila from death. Although the servants of Yamaraja have jurisdiction over all sinful living beings, the messengers of Visnu are capable of punishing even Yamaraja himself if he wrongs a devotee of Visnu, the Supreme Lord.
When the Yamadutas were thus forbidden from carrying out their mission, they exclaimed, "Who are you, sirs, that you dare challenge the jurisdiction of Yamaraja? Whose servants are you, where have you come from, and why are you forbidding us to touch Ajamila? You may know that this man Ajamila did not undergo atonement for his sins. Because of his sinful life, we must take him to the court of Yamaraja for punishment. According to the extent of his sinful acts, he will be punished and thus purified."
The Visnudutas replied, "Ajamila has already atoned not only for sins performed in one life but for those performed in millions of lives, for in a helpless condition he chanted the holy name of Narayana. Although he was calling his son's name, he nevertheless uttered the four syllables na-ra-ya-na. Simply by chanting the name of Narayana in this way, he sufficiently atoned for the sinful reactions of millions of lives.
"Chanting the holy name of the Lord is the best process of atonement for a thief, for a drunkard, for one who betrays a friend or relative, for one who kills a brahmana, for one who indulges in sex with the wife of his guru or another superior, for one who murders women, the king or his father, for one who slaughters cows and for all other sinful men. Simply by chanting the holy name of the Lord, such sinful persons can attract His attention.
At the time of death, Ajamila helplessly and very loudly chanted the holy name of the Lord. That chanting alone has already freed him from the reactions of all sinful life. Therefore, O servants of Yamaraja, do not try to take him to your master for punishment. One who chants the holy name is immediately freed from the reactions of unlimited sins, even if he chants indirectly to indicate something else, jokingly, for musical entertainment, or even neglectfully. This is accepted by all the learned scholars of the scriptures. If one chants the holy name of Hari and then dies because of an accidental misfortune, one is immediately absolved from having to enter hellish life, even though he is sinful.
"Although one may neutralize the reactions of sinful life through austerity, charity, vows and other such methods, these pious activities cannot uproot the material desires in one's heart. However, if one serves the Personality of Godhead, he is immediately freed from all such contaminations. If a person unaware of the effective potency of a certain medicine takes that medicine, it will act even without his knowledge because the medicine's potency does not depend on the patient's understanding. Similarly, even though one does not know the value of chanting the holy name of the Lord, if one chants knowingly or unknowingly, the chanting will be very effective."
The Visnudutas released the brahmana Ajamila from the bondage of the Yamadutas and thus saved him from death. Ajamila, now free from fear, came to his senses and immediately offered respects to the Visnudutas by bowing his head at their feet. He was extremely pleased by their presence, for they had saved him from the Yamadutas. When the Visnudutas saw that Ajamila was attempting to say something, they suddenly disappeared.
Having heard the discourse between the Yamadutas and the Visnudutas, Ajamila could now understand the transcendental religious principles concerning the relationship between the living being and the Supreme Personality of Godhead. Furthermore, since Ajamila had heard their glorification of God, he was now a perfectly pure devotee and could thus remember all his past sinful activities, which he greatly regretted having performed.
I am such a sinful person," he said. "But now since I have this opportunity, I must completely control my mind, life and senses and always engage in devotional service so that I may not again fall into the deep darkness and ignorance of material life. Because I identified myself with my body, I was subjected to desires for sense gratification and thus performed many pious and impious acts. That was my material bondage.
But now I shall disentangle myself from that bondage. Being a most fallen soul, I was victimized by illusions and was like a dancing dog led around by a woman's hand. Now I shall give up these lusty desires and free myself from these illusions. I shall become a merciful, well-wishing friend to all living entities and always absorb myself in Krsna consciousness."
Because of a moment's association with devotees of the Supreme Lord, Ajamila was able to detach himself with determination from the material conception of life. Freed from all material attraction, he immediately started for the city of Hardwar on the Ganges River. In Hardwar, Ajamila stayed at a Visnu temple, where he executed bhakti-yoga, the process of devotional service. He controlled his senses and fully applied his mind in the service of the Lord. When his intelligence and mind were fixed on the form of the Lord, Ajamila once again saw before him the four celestial messengers of Visnu. Recognizing them as the ones he had seen previously, he offered his respects by bowing down before them.
The Visnudutas had gone away for some time to allow Ajamila to become firmly fixed in meditation on the Lord. Now that his devotion had matured, they returned to take him back to the spiritual world. Upon seeing the Visnudutas, Ajamila gave up his material body and regained his original spiritual body, which was just suitable for an associate of the Lord. This is the result of perfection in Krsna consciousness: after giving up the material body, one is immediately transferred to the spiritual world in one's original spiritual body to become an associate of the Supreme Personality of Godhead.
Accompanied by the Visnudutas, Ajamila then boarded a golden airplane that took him through the airways, directly to the abode of the Supreme Lord Visnu, the husband of the goddess of fortune. Although he had been destined to be carried away to hell by the Yamadutas, Ajamila was saved by the Visnudutas and was able to go back home, back to Godhead, because he chanted the holy name of the Lord at the time of his death. Therefore if a person faithfully and inoffensively chants the holy name of the Lord, who can doubt that he will return to Godhead?
At the time of death one is certainly bewildered because his bodily functions are in disorder. At that time, even one who throughout his life has practiced chanting the holy name of the Lord may not be able to chant the Hare Krsna mantra very distinctly. Nevertheless, such a person receives all the benefits of chanting the holy name. While the body is fit, therefore, we should chant the holy name of the Lord loudly and distinctly. If one does so, it is quite possible that even at the time of death he will be able to properly chant the holy name of the Lord with love and faith and return home, back to Godhead, without a doubt.