Praise be to Allaah.
It is prescribed to visit graves in order to learn a lesson from that and to remember the Hereafter. That is subject to the condition that one does not say anything that will anger their Lord, such as calling upon the one who is buried or seeking his help instead of Allaah, or praising him and saying that he is for certain in Paradise, or doing actions of bi’daa (innovation) etc.
The purpose of visiting the graves is two-fold:
(a) The visitor benefits from remembering death and the dead, remembering that their destiny will be either Paradise or Hell. This is the primary purpose of the visit.
(b) The deceased also benefits and is treated kindly by the visitor greeting him with salaams, making du’aa’ for him, praying for forgiveness for him. This applies only to Muslims.
It is permissible to raise the hands when reciting this du’aa’
Because of the hadeeth of ‘Aa’ishah who said: “The Messenger of Allaah (peace and blessings of Allaah be upon him) went out one night, and I sent Bareerah to follow him and see where he went. She said, ‘He went towards Baqee’ al-Gharqad [the graveyard in Madeenah], and he stood at the bottom of al-Baqee’ and raised his hands, then he went away.’ Bareerah came back to me and told me, and when morning came I asked him about it. I said, ‘O Messenger of Allaah, where did you go out to last night? He said, ‘I was sent to the people of al-Baqee’, to pray for them.’”
Is it forbidden to face the grave when saying du’aa’ for the deceased?
Shaykh ‘Abd al-‘Azeez ibn Baaz (may Allaah have mercy on him) replied:
That is not forbidden, rather one may say du’aa’ for the deceased facing the qiblah or facing the grave, because the Prophet (peace and blessings of Allaah be upon him) stood over graves after the burial and said: “Pray for forgiveness for your brother and ask that he be made steadfast, for even now he is being questioned.” (Narrated by al-Bukhaari) And he did not say to face the qiblah, so both are permissible, whether one faces the qiblah or the grave. The Sahaabah (may Allaah be pleased with them) prayed for the deceased when they were gathered around the grave. [13/338]
You should not walk between the graves of the Muslims wearing your shoes
It was narrated that ‘Uqbah ibn ‘Aamir (may Allaah be pleased with him) said: “The Messenger of Allaah (peace and blessings of Allaah be upon him) said: ‘If I were to walk on hot coals or on a sword, or if I were to mend my shoes using my feet, that would be better for me than if I were to walk on the grave of a Muslim. And it makes no difference to me if I were to relieve myself in the midst of the graves or in the middle of the market-place [i.e., both are equally bad].’” (Narrated by Ibn Maajah, 1567)
We ask Allaah, the Most High, the Omnipotent, to have mercy upon our dead and the deceased Muslims.
Adapted from Islam Q&A