what you are referring to here is called ‘merit transfer’. The idea is that through your skilful conduct you build up a stock of merit - a kind of spiritual capital that is earning interest in your karmic bank account. According to many Buddhist traditions, this merit may be transferred to others, especially the dead, sick, or spiritually destitute.
There are specific ceremonies and rituals by means of which merit is transferred. For instance, in Theravada Buddhism, a donor may give food or clothing to the monastic community and then ‘donate’ the merit gained to a departed relative (to help them in the ‘after-life’). As far as I know, there is no need for the other person to agree to receive merit.
Some Mahayana traditions emphasise merit transfer as a means of overcoming spiritual acquisitiveness and expressing compassionate concern for others. The Bodhisattva, or ideal Buddhist, gives all his or her merit away because they want as many beings as possible to benefit from their spiritual virtue. A form of words that one might use to dedicate merit in this way is: ‘May the merit gained in my acting thus, go to the alleviation of the suffering of all beings.’