When man dies
The Prophet (sal Allahu alaihi wa sallam) said, “When a man dies, his deeds come to an end, except for three: A continuous charity, knowledge by which people derive benefit and a pious son who prays for him.”
A thoughtful reflection of this Hadith will reveals that the three things are indeed man’s own good deeds which somehow continue to exist even after his death. For example, places like a hospital, a mosque, a religious school and a well built by a man continue to operate like a trust as traces of his activity in earthly life, Allah says (interpretation of the meaning):
“And We record that which they send before (them), and their traces [their footsteps and walking on the earth with their legs to the mosques for the five compulsory congregational prayers, Jihad (holy fighting in Allah's Cause) and all other good and evil they did, and that which they leave behind].” (36:12)
So long as they exist and people benefit from them, the dead person will keep receiving his due reward.
The same is pertinent to the spread of knowledge of Islamic disciplines to people’s moral uplift. For instance, if during his lifetime he wrote books based on Qur’an and Hadith, advocated the cause of Islam, instructed lots of pupils in Islamic teachings, it will be counted as an unending activity on his part. The Prophet (sal Allahu alaihi wa sallam) said, “Whoever guides [another] to a good deed will get a reward similar to the one who performs it.” [Sahih Muslim]
Besides, this Hadith tells us that, after his death, man will receive reward by the Du’aa (Supplications) of his virtuous children, and other Ahadith confirm it. This clears the issue of sending the fruit of good works to the welfare of a dead person’s soul. Charity or the continual charity from the part of a dead person himself, beneficial (religious) knowledge and prayer are acts of Sunnah under this rule. With the exception of these, all other deeds do not benefit the dead. However, if the dead person was under obligation to perform Hajj or had missed obligatory or vowed fasts, his heirs are commanded by the Prophet (sal Allahu alaihi wa sallam) to observe them on his behalf. Because this is like a debt payable by the dead person and his heirs are under obligation to discharge it for him. Yet, this rule is inapplicable to other forms of physical worship.
And Allah knows best!