How does it work? One explanation is that the patient has inspired another person, whether consciously or not, to perform a mitzvah and thereby grow closer to God. Therefore, the patient has a direct merit for being the “cause” of this result, thereby deserving positive treatment from Above.
Another explanation is that a person who prays for another’s wellbeing is binding himself to the patient, showing concern and “sharing” the patient's pain. The Divine calculation now takes into account the welfare of all those who are praying for that patient in determining his state of health.
According to both of these explanations not only can the merit of prayer have a positive effect on the recovery of the sick person. Torah study, giving charity and other good righteous deeds performed for the merit of the sick person can also add to the merits of the sick person and may possibly affect his recovery.
The earliest source in Jewish texts where prayer for the sick is mentioned is when Abraham prays for Avimelech (Gen. 20:17). Later in the Torah, Moses prays for the recovery of Miriam, his sister (Num. 12:13). There are numerous places in the Prophets as well where prayer for another person facilitates his recovery.
The Talmud (Nedarim 40a) discusses the obligation to visit the infirm and to pray for them. In fact, the implication is that prayer is an essential ingredient of the visit. If one visits a sick person but does not pray for him, one has not fulfilled the mitzvah. The obligation to pray for the sick is found in The Code of Jewish Law (Y.D. 355), which states that when praying, one should ask that this sick person be healed along with the other sick people of Israel. In addition, the central prayer in Judaism — the shemoneh esreh — includes a blessing in which we pray for those who are not well.