The Gemora states that Rabbi Bana’ah used to mark out caves and it relates what happened when he went into the Cave of Machpeilah (where the Patriarchs and Matriarchs were buried).
The Rashbam explains that he did this in order to determine the precise dimensions of the crypts, and after marking its borders on the ground above, people, and especially Kohanim, would be able to avoid becoming tamei (through tumas ohel – forming a tent over a corpse).
Tosfos asks from the Gemora in Yevamos (61a): Rabbi Shimon ben Yochai said: The graves of idolaters do not transmit tumah through the roof (if the tumah source and a person or object is under the same roof). If so, how could there be any tumah from the Cave of Machpeilah?
Tosfos continues that even according to the Rabbis, who disagree and hold that there is tumas ohel from an idolater’s grave, nevertheless, regarding a grave before the Giving of the Torah, the Gemora in Nazir (54a) only includes it for tumah with respect to touching, but not through roof association!?
The Ramban explains that Rabbi Bana’ah did this out of respect to our forefathers, for although they cannot transmit tumah, they accepted and observed the entire Torah.
Reb Chaim Brisker writes that if not for these Rishonim, he would have said that a corpse which is in a grave after the ?Giving of the torah is considered as if it was freshly buried, and it will transmit tumah.
Tosfos answers that the reason idolaters are excluded from tumas ohel is because it is written [Yechezkel 34:31]: Now you my sheep, the sheep of my pasture; you are adam. You, Israel, are referred to as “Adam,” man, but an idolater is not regarded as “Adam.” [The word “Adam” is the term used in the Torah regarding the laws of tumah by way of a roof; thus we see that the grave of an idolater does not transmit this tumah.] However, we find that Avraham Avinu was referred to as “Adam,” and Adam Harishon as well; accordingly, the halachos of tumas ohel would apply to the Cave of Machpeilah.
According to these Rishonim, our Gemora would seemingly be a proof that the graves of the righteous transmit tumah.
Tosfos in Bava Metzia (114b) writes that when Eliyahu said that the reason he was involved in the burial of Rabbi Akiva (although he was a Kohen) was because there is no tumah by a Torah scholar, that was only an excuse; the real reason was because the corpse had to be treated like a “meis mitzvah,” for everyone else was too frightened (from the government) to bury him.
There are, however, some Acharonim who rule that the righteous do not transmit tumah. Over the ages, some Kohanim have relied on this to attend the funerals of tzadikim (see Shut Minchas Eliezar 3:64). However, the vast majority of poskim have not relied on this Midrashic statement and forbid Kohanim from attending the funeral of tzadikim (See discussion in Beit Yosef YD 373; Pischei Tshuva YD 372:2; Kitzur Shulchan Aruch 202:14; Bach YD 374; Shut Divrei Yatziv (by Klausenburg rebbe) YD:231; Yechave Daat 4:58).
Rabbi Gil Student cites other halachic authorities who deal with this topic. The contemporary greats, including Rav Moshe Feinstein, Rav Yaakov Kamenetzky, Rav Yaakov Yitzchak Ruderman, Rav Yitzchak Hutner, and Rav Shlomo Zalman Auerbach all come down as prohibiting in their letters of approbation to the book Ziyon L'nefesh Zvi. [See footnotes 50 and 51 to Al Hadaf Kesubos 7/No.65/July 2 '00.] The only exception would be the actual Nasi, for whom the Shulchan Aruch (YD 374:11) says all (even Kohanim) may become tamei.
[See the responsum on this topic of graves of tzadikim causing tumah in Eliyav ben Achisamach (written by Rav Sender Friedenberg, formerly Rav of Prashvitz and then of Bastravtza, in 5671). See also the specific responsa of Rav Shlomo Kluger (1785-1869; Tuv Taam v'Daas 2:Aveilus:231) who ruled that Kohanim could not go near the grave of the great chassidic Rebbe Rav Aaron of Chernobil, or of any other tzadik. See Kitzur Shulchan Aruch 202:14) that "Kohanim hedyotim" rely on this rule to visit the graves of the righteous, but they are mistaken and one should correct them.]
The Kaftor va’Ferech writes that Rabbi Bana’ah marked these graves in order for the future generations to know where our forefathers were buried, and this way, we would be able to pray by their gravesite that no tragedies should befall Klal Yisroel. This would be just as Calev separated himself from the plan of the spies and went and prostrated himself upon the graves of the Patriarchs, saying to them, “My fathers, pray for mercy on my behalf that I may be spared from the plan of the spies.”
The Ritva writes that although the Gemora in Taanis (16a) states that it was the custom to visit a cemetery on a fast day, they didn’t go to the cemetery in order to daven there because that is forbidden on the account of “loeg lerosh” – it is considered mocking to the dead who cannot perform the mitzvos; rather they davened in the streets and went to the cemetery afterwards. The Ran adds that they did not take the sefer Torah with them when they went to the cemetery.
The Noda B’yehuda (O”C 2:109) was asked on a year that there was no rain and there was tremendous suffering; if they would be permitted to go to a cemetery with a sefer Torah and daven there for rain.
He cites a Zohar (Acharei Mos) which states that davening by a cemetery inspires the souls of those buried there to inform those that are buried in Chevron (Patriarchs and the Matriarchs) who subsequently will arouse Hashem’s compassion.
However, there is a Gemora in Brochos (18a) which rules that a person should not enter a cemetery with tefillin on his head or read from a sefer Torah in his arm. We can infer from this Gemora that reading from the sefer Torah is forbidden but holding it would be permitted. The Kesef Mishna in Hilchos Sefer Torah (10:6) learns that both are forbidden; reading from the sefer Torah or holding it.
The Noda B’yehuda concludes that although he is not an expert in the hidden portions of Torah, the Zohar cited does warn against bringing a sefer Torah that might be missing letters into a cemetery since this can cause terrible consequences.
The sefer Igra D’taanisa wonders why the Noda B’yehuda makes no mention of the Gemora in Taanis, which would indicate that one can go daven by a cemetery.
The Minchas Elozar discusses the permissibility of people davening by Kever Rochel. Some say that we are not mocking Rochel since she was living before the Torah was given; she was never obligated in mitzvos.
The Netziv rules that in his days, it would be permitted because the custom was to bury them deeper than ten tefachim from the ground and it is considered like a different domain.
The Rama (O”C 581:4) writes that there are places that have the custom to go to cemeteries on Erev Rosh Hashanah and to recite lengthy Tefillos there. The Chidah asks on this Rama from the Ritva in Taanis that states explicitly that one should not daven in the cemetery.
There are those that create a distinction between a compulsory tefillah and a tefillah which is only voluntary.
The Elya Rabbah (581) quotes from the Maharil that one should be careful when going to the graves of Tzadikim that your tefillos should not be directed towards those that are buried there, rather one should daven to Hashem and ask for compassion in the merit of these Tzadikim. Some say that you can ask the dead to be an advocate on your behalf.
The Bach (Y”D 217) rules that it is forbidden to daven to the dead because of the prohibition of being “doresh el hameisim.” He points out that even though we find that Calev did daven in Chevron by the Meoras Hamachpeila, he wasn't davening to the Avos. Rather, since a cemetery is a place of holiness and purity, the tefillos davened there will be more readily accepted.