We find in the Talmud and Midrash two sources showing that praying at the graves of the righteous is beneficial. The first is the Sages’ remark that Kaleb, one of the spies sent by Moshe to tour Eretz Yisrael, prayed at the graves of the patriarchs in Hebron. The Torah tells that when the spies went on their excursion, "Va’yabo Ad Hebron" – "He came to Hebron," implying that just one of the spies went to that city. The Talmud explains that this refers to Kaleb, who went to pray at Me’arat Ha’machpela to beseech G-d to give him the strength he would need to oppose the scheme of his fellow spies. (Rav Hai Havita Ha’kohen of Djerba noted that the letters of the word "Va’yabo" represent the phrase "Be’kibreh Abraham Yishak Ve’Yakaob" – "at the graves of Abraham, Yishak and Yaakob.")
Secondly, the Midrash tells that at the time when G-d destroyed the Bet Ha’mikdash, he urged the prophet Yirmiyahu to go to Hebron and beg the patriarchs to plead to G-d on behalf of their descendants, and Yirmiyahu indeed went to Me’arat Ha’machpela to pray.
The Rama (Rav Moshe Isserles of Cracow, 1530-1572) writes that it is customary to visit the graves of righteous people on Ereb Rosh Hashanah and pray there. The Be’er Heteb commentary explains that the gravesites of pious people are sacred, and prayers recited at those locations are more readily accepted. Therefore, before Rosh Hashanah, it is appropriate to pray at these sites to beseech G-d for a favorable judgment.
Rav Nissim Peretz (Bnei-Brak, 1946-2012) was asked whether there is value in going through the trouble to visit the graves of Sadikim, instead of just staying in one’s place and learning Torah. He explained that the special sanctity of these gravesites makes such visits exceedingly impactful, and therefore, at the right times, this is something which is very significant and worthwhile for one to do.
Two different opinions exist as to how precisely a person should pray at the grave of a Sadik. The Be’er Heteb, in the aforementioned passage, writes that one should not address the Sadik while praying at his gravesite. The concept of praying at a righteous person’s grave is that we invoke his merit as we beseech G-d to help us and grant us our wishes. By contrast, the Peri Megadim (Rav Yosef Teomim, 1727-1793) writes (581), citing the work Ma’aneh Lashon, that when we pray at a righteous person’s grave, we should ask the Sadik to intercede on our behalf. According to this opinion, we indeed speak to the Sadik, asking him to serve as our advocate before G-d.
Before praying at the gravesite, it is proper to give some charity and pray for the benefit of the righteous person’s soul.
Rav Eliezer Papo (1785-1828) writes in his work Hesed La’alafim that according to Kabbalistic tradition, it is proper to put one’s left hand on a righteous person’s grave, and not the right hand.
Among non-Jews, it is customary to place flowers on a person’s grave. Hacham Ovadia Hedaya (1889-1969), in his Yaskil Abdi, strongly opposes this practice, arguing that it was brought to Israel by the Europeans, and actually constitutes Kefira (heresy). Adorning the grave with flowers, he writes, expresses the view that death is joyous and festive like a wedding, when in truth a person who passes on faces judgment for his conduct during his life. Placing flowers on a grave gives the impression that there is no judgment, and that everyone goes straight to the bliss of the Gan Eden, without being judged. Hacham Ovadia Yosef, on the other hand, writes that this practice is not forbidden, though it should be discouraged. He found two sources in the Gemara indicating that there was once a custom to place Hadasim (myrtle branches) on graves, thus proving that this is not a strictly gentile practice that is thus forbidden. Nevertheless, since it is not customary among Jews, Hacham Ovadia writes that we should politely and respectfully try to convince people not to observe this practice.
A number of Poskim addressed the question of how reconcile the widespread practice to pray and learn at the gravesites of Sadikim with the explicit ruling of the Shulhan Aruch (Orah Haim 71) forbidding performing Misvot in a cemetery. Performing Misvot in the presence of the deceased is considered "Lo’eg La’rash" ("taunting the pauper"), as though we’re showing that we are capable of performing Misvot and they aren’t, and is thus prohibited. How, then, is it permitted to pray and study Torah at gravesites of righteous people?
Different theories have been proposed to answer this question. The Minhat Elazar (Rav Haim Elazar Spira of Munkatch, 1868-1937) suggests that the prohibition of "Lo’eg La’rash" applies only at the graves of people who were obligated to perform Misvot during their lifetime. Therefore, it does not apply to the graves of the patriarchs, and of our matriarch Rachel, who lived before Matan Torah and were thus not obligated in Misvot. (Of course, this does not justify praying and learning at the gravesites of other Sadikim, who lived after Matan Torah, such as Rabbi Shimon Bar Yohai and Rabbi Meir.) The Minhat Elazar adds that in most cases, the area where people pray is distant from the body, which is usually far below the place where visitors come and stand, and so their prayers do not violate the prohibition of "Lo’eg La’rash."
Others explain that anything done for the purpose of the soul of the deceased does not violate "Lo’eg La’rash." In fact, tradition teaches that after the passing of King Hizkiyahu, a yeshiva was built at his gravesite in his honor. Learning and praying for the benefit of the deceased’s soul helps him, and thus is not considered "taunting" in any way. However, this fails to explain why it is permissible to pray Minha, for example, at a gravesite, as this is a standard prayer, and not a special prayer for the deceased’s soul.
Interestingly, it is told that when Rav Shmuel Salant (Jerusalem, 1816-1909) was present at a gravesite and a Minyan was formed for prayer, he would move off to the side, so as not to pray right next to the grave.
In any event, as Hacham David Yosef notes in his Halacha Berura, the practice to pray at the gravesites of Sadikim is widely accepted, notwithstanding the potential issue of "Lo’eg La’rash."
Summary: There is a longstanding, well-documented tradition to pray at the gravesites of righteous people, which are considered sacred places, such that prayers at these sites are especially powerful and effective. According to some opinions, when praying at the gravesite of a Sadik one should ask G-d for help in the merit of the Sadik, and according to others, one should ask the Sadik’s soul to advocate on his behalf before G-d. Before praying at a Sadik’s gravesite, it is proper to give charity and learn Torah for the benefit of the Sadik’s soul. One should not place flowers on a gravesite.