Read what Torah.org says on the topic:
http://www.torah.org/advanced/weekly-halacha/5760/shlach.htmlVISITING GRAVES of TZADDIKIM: HOW and WHY?
The ancient minhag yisrael of visiting and davening at graves of tzaddikim during times of tribulation has many sources in Talmudic literature(1). Indeed, Shulchan Aruch records in several places that it is appropriate to do so on certain public fast days in general(2) and on Tishah B'av after midday in particular(3). Erev Rosh ha-Shanah, too, is a day when it has become customary to visit graves(4). But what is the reason for this? How does it help us(5)?
The Talmud(6) cites two explanations: 1) To serve as a reminder of man's immortality so that one repent while he still can; 2) To ask the dead to pray for mercy on our behalf. [A practical difference between these two reasons, says the Talmud, is whether or not it is appropriate to visit graves of non-Jews [when there are no Jewish graves near by] since even a non-Jew's grave reminds man of his immortality. Nowadays, however, when non-Jews mark their graves with religious symbols, it is no longer appropriate to visit non-Jewish graves even if there are no Jewish graves in the area(7).]
The second reason quoted in the Talmud - to ask the dead to pray for mercy on our behalf - demands clarification. Many people assume that this means that we are allowed to pray to the dead to ask them to help us. This is a serious mistake and strictly forbidden. One who prays with this intent transgresses the Biblical(8) command of "You shall not recognize the gods of others in my presence"(9). It may also be a violation of the Biblical(10) command against "one who consults the dead"(11).
If so, what does the Talmud mean when it says that we "ask the dead to beg for mercy on our behalf"? We find two schools of thought concerning this matter:
Some(12) hold that it means that it is permitted to speak directly to the dead to ask them to daven to Hashem on our behalf. This is similar to the prayers that we find throughout Selichos which are addressed to the malachim. Although the malachim - who are merely G-d's messengers - do not posses the ability to do anything of their own accord, still we may ask them to "deliver" our prayers to Hashem. So, too, it is permitted to address the dead directly and ask them to intercede on our behalf at the heavenly throne.
Others(13) strongly disagree and maintain that this, too, is strictly forbidden. In their opinion, addressing a dead person is a violation of "consulting the dead". What the Talmud means by "asking the dead to pray for mercy on our behalf" is that we daven directly to Hashem that in the merit of the dead He should have mercy on us. We visit the graves only to remind Hashem of the merits of the holy tazddikim who are interred there.
The practical halachah is as follows. Most of the classical poskim(14) rule in accordance with the second view. Mishnah Berurah(15) also clearly writes: We visit graves because a cemetery where tzaddikim are interred is a place where prayers are more readily answered. But one should not place his trust in the dead. He should just ask Hashem to have mercy on him in the merit of the tzaddikim who are interred here.
But other poskim rule that it is permitted to talk to the dead [or to angels] to intercede on our behalf. In a lengthy responsum, Minchas Elazar(16) proves from a host of sources throughout the Talmud and Zohar that not only is this permitted but it is a mitzvah to do so.
But as we said before, all opinions - without exception - agree that it is strictly forbidden to daven directly to a dead person [or to angel] so that they should help us. The most that is permitted [according to the lenient views] is to ask them to act as our emissaries to Hashem, so that Hashem will look favorably and mercifully upon us.
THE VISIT: PROPER CONDUCT
Upon entering a cemetery, the blessing of Asher yatzar eschem badin is recited(17). The full text is found in many siddurim. This blessing is recited only once within any thirty day period(18).
Before visiting at a grave, one should wash his hands(19).
Upon reaching the grave, one should place his left hand on the marker(20).
It is forbidden, though, to lean on it(21).
Within four amos [7-8 feet] of a grave(22):
The tzitzis strings should be concealed(23).
Levity, eating, drinking, greeting a friend or engaging in business is prohibited(24).
Learning, davening or reciting a blessing is prohibited(25). Many poskim, however, hold that it is permitted to recite Tehillim(26) or the burial Kaddish(27).
One should be careful not to step on any grave(28).
Before taking leave of a grave it is customary to put a stone or some grass on the marker(29).
The same grave should not be visited twice in one day(30).
Upon leaving the cemetery, it is customary to take some soil and grass from the ground and throw it over one's shoulder(31). There are many different reasons for this custom. On Shabbos, Yom Tov and Chol ha-Moed this may not be done(32).
After leaving a cemetery and before entering one's home(33) or another person's home(34), one should wash his hands three times from a vessel, alternating between the right and left hands(35). There are different customs concerning the method of washing(36): The water should drain into the ground and not collect in a puddle. After washing, any water that remains in the vessel is poured out. The vessel is turned upside down and placed on the ground, not handed to the next person(37). Some let their hands air dry and do not use a towel(38). Some wash their face as well(39).
FOOTNOTES
1 Yosef cried at his mother's grave before going to Egypt (Sefer ha-Yashar); Before being exiled, the Jewish people wept at Kever Rochel (Rashi, Vayechi 48:7); Kalev prayed at Meoras ha-Machpeilah before confronting the meraglim (Sotah 34b). See also Ta'anis 23b.
2 O.C. 579:3.
3 Rama O.C. 559:10.
4 Rama O.C. 581:4. Some go on Erev Yom Kippur as well (Rama O.C. 605:1) while others oppose going on that day; Elef ha-Magen 605:39 quoting Ya'avetz; Divrei Yoel 99:4.
5 Our discussion focuses on visiting graves on fast days and at other times of strife. Do not confuse this with the custom of visiting graves of parents and other relatives (on their yahrzeits and other occasions) whose primary purpose is to elevate the soul of the deceased and to give it "pleasure".
6 Ta'anis 16a.
7 Mishnah Berurah 579:14. See also Kaf ha-Chayim 559:81.
8 Shemos 20:3.
9 Sefer ha-Ikarim, mamar 2, quoted in Gesher ha-Chayim 2:26.
10 Shoftim 18:11.
11 Eliyohu Rabbah 581:4.
12 See Shalah (quoted by Elef ha-Magen 581:113), Pri Megadim O.C. 581:16 and M'haram Shick O.C. 293.
13 The source for this view among the Rishonim is Teshuvos R' Chaim Paltiel (quoted by the Bach and Shach Y.D. 179:15) and Maharil, Hilchos Ta'anis, (quoted by Be'er Heitev O.C. 581:17).
14 Including the Be'er Heitev, Chayei Adam, Mateh Efrayim and Kitzur Shulchan Aruch.
15 581:27.
16 1:68. See also Gesher ha-Chayim 2:26 and Minchas Yitzchak 8:53.
17 O.C. 224:12. This blessing is recited only at a burial plot that contains at least two graves.
18 Mishnah Berurah 224:17.
19 Mishnah Berurah 4:42.
20 Kitzur Shulchan Aruch 128:13. See there also for the text that should be recited at that time.
21 Shach Y.D. 363:3.
22 Note that according to the Arizal (quoted by Mishnah Berurah 559:41) one should never go within four amos of a grave [except at interment]. In Igeres ha-Gra he writes that one should never enter a cemetery at all, and especially not women. [It is commonly accepted that a woman who is a niddah does not go to a cemetery at all (Mishnah Berurah 88:7). Under extenuating circumstances a rav should be consulted; see Beis Baruch on Chayei Adam 3:38.]
23 Mishnah Berurah 23:3. Tefillin, too, must be concealed.
24 Y.D. 368:1; Rama Y.D. 343:2.
25 Y.D. 367:3; 368:1.
26 Birkei Yosef Y.D. 344:17.
27 Gesher ha-Chayim 1:16-4.
28 Taz Y.D. 363:1.
29 Be'er Heitev O.C. 224:8.
30 Mishnah Berurah 581:27.
31 Y.D. 376:4. Some do this only after an interment.
32 O.C. 547:12.
33 Kaf ha-Chayim 4:80.
34 Mishnah Berurah 4:43. It is permitted, however, to enter a shul or another public place before washing; Harav M. Feinstein (Moadei Yeshurun, pg. 58).
35 Mishnah Berurah 4:39.
36 Some of these customs do not have an halachic source; they are based on Cabalistic writings and customs.
37 R' Akiva Eiger (Y.D. 376:4). See Zichron Meir, pg. 450.
38 Several poskim write that this does not apply during the cold winter month when the hands will become chapped; see Kaf ha-Chayim 4:78.
39 Mishnah Berurah 4:42.