Firstly: The Type of Visits
Visiting the graves is of three types:
1. Legislated visit
This is the visit which is intended to supplicate for the deceased, seek forgiveness for them, and remind oneself of the afterlife.
2. Innovated visit
This is when the graves are visited with the intention to perform acts of worship at their sight, whether through recitation, prayer, or slaughter, believing that worship there is special. This is an innovation, and it is a stepping-stone to polytheism.
3. Polytheistic visit
This is the type of visit where the dead are called upon and invoked for salvation, or to slaughter in their name, or other acts of worship which are only befitting of Allah. This is polytheism.
Secondly: The Ruling of Men Visiting the Grave
It is mustahabb for men to visit the graves, and this is by agreement of the four schools of jurisprudence.
Thirdly: The Ruling of Women Visiting the Grave
Scholars have differed over the ruling of women visiting the graves  Ibn Taymiyyah said: “The jurists said: if the woman knows that by her visiting the graveyard, she will either do or say that which is inappropriate, then it is haram for her to visit without disagreement.” Majmu` al-Fatawa (356/24) according to three views:
The first: is that it is makruh for them to visit the graves. This is the position of the Shafi`is, Hanbalis, a position among the Hanafis, and a position among the Malikis.
The second: is that it is haram for them to do so. This is a position among Hanafis, a position among Shafi`is, and it is the position of Ibn Taymiyyah, Ibn Baz, and Ibn `Uthaymin.
The third: is that it is permissible for women to visit the graves. This is the position of the Hanafis, a position among Malikis,  They conditioned that the woman is old, no longer seeking marriage nor sought after for it. a position among Shafi`is,  They conditioned safety from any temptation. and a narration from Ahmad. It is also the choice of al-Qurtubi from the Malikis,  He conditioned permissibility with women going separately from the men and avoiding temptation. as well as al-Shawkani.  With the condition of avoiding temptation, and not neglecting the right of the husband, while leaving off beautification and screaming, and the like.
Fourthly: The Rulings of Visiting the Grave of the Disbeliever
It is permissible for the Muslim to visit the grave of the disbeliever. This is the position of the Shafi`is, Hanbalis, and Ibn Hazm. It is the choice of Ibn Taymiyyah, Ibn Baz, and Ibn `Uthaymin.
Fifthly: Utterances when Visiting the Graveyards
It is mustahabb that the visitor greets the graveyard and supplicates for the one he is visiting and all those buried there. This is by agreement of the four schools of jurisprudence.