The mother has more right to custody of her children until the age of seven, so long as she has not remarried, because of the report narrated by Ahmad (6707) and Abu Dawood (2276), from ‘Abdullah ibn ‘Amr, according to which a woman said: O Messenger of Allah, this is my son; my womb was a vessel for him, and my breasts gave him to drink, and my lap was a protection for him. His father has divorced me and he wants to take him away from me. The Messenger of Allah (blessings and peace of Allah be upon him) said: “You have more right to him so long as you do not remarry.”
This hadeeth was classed as hasan by al-Albaani in Saheeh Abi Dawood.
If she remarries, custody passes to the one who comes after her. There is a difference of opinion among the fuqaha’ concerning that. Some of them think that it passes to the maternal grandmother and this is the opinion of the majority of the four madhhabs. Some think that it passes to the father, and this is the view favoured by Shaykh al-Islam Ibn Taymiyah and Ibn al-Qayyim. See al-Masoo‘ah al-Fiqhiyyah, 17/303; ash-Sharh al-Mumti‘, 13/535.
It says in Zaad al-Mustaqni‘: The one who is most entitled to it (custody) is the mother, then her foremothers, the closest then the next closest, then the father, then his foremothers in like manner, then the grandfather, then his foremothers in like manner…
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said in his commentary: This sequence that was mentioned by the author is not based on any foundation of evidence and there is no rational explanation for it, and there are some contradictions in it that make one feel uneasy about it. Hence the scholars differed concerning the sequence of (who is entitled to) custody and there are several opinions, but none of them has any reliable basis. Hence Shaykh al-Islam (may Allah have mercy on him) was of the view that custody should go to the one who is most closely related, whether that is the father or mother, or on the father’s side or on the mother’s side; if they are equal, then priority should be given to the female. If there are two males or two females then lots may be drawn, otherwise the father’s side takes precedence.
End quote from ash-Sharh al-Mumti‘, 13/535
However, even though the father may take precedence over the maternal grandmother, it is still essential to examine the father’s situation and how suitable and able he is to raise the child. If he is corrupt or incapable of raising the child, then custody passes to someone else. These are matters that should be solved through the intervention of good and righteous people in order to choose what is best for the child and less harmful to both the husband and wife.
If the mother has not remarried and the child has reached the age of seven years:
If the child is male, he should be given the choice between his father and mother and then live with the one he chose, because of the report narrated by an-Nasaa’i (3496) and Abu Dawood (2277), according to which a woman came to the Messenger of Allah (blessings and peace of Allah be upon him) and said: May my father and mother be sacrificed for you. My husband wants to take my son away even though he benefits me and brings me water from the well of Abu ‘Anbah. Her husband came and said: Who is disputing with me concerning my son? [The Prophet (blessings and peace of Allah be upon him)] said: “O boy, this is your father and this is your mother, take the hand of whichever of them you want.” And he took his mother's hand and she went away with him.
This hadeeth was classed as saheeh by al-Albaani in Saheeh Abi Dawood. This is the opinion of the Hanbalis and Shaafa‘is
If the child is female, then she is also to be given the choice according to ash-Shaafa‘i (may Allah have mercy on him).
Abu Haneefah said: The mother is more entitled to custody until the girl gets married or starts to menstruate.
Maalik said: The mother is more entitled to custody until she gets married and the husband consummates the marriage with her.
Ahmad said: The father is more entitled to custody, because it is more appropriate for the father to take care of her.
See: al-Mawsoo‘ah al-Fiqhiyyah, 17/314-317
If the child has reached the age of maturity that he may choose whichever of his parents he wants.
Ibn Qudaamah (may Allah have mercy on him) said:
There is no proof of custody except in the case of a child or one who has a disability. As for the adult of sound mind, there is no custody of him and he has the choice of staying with whichever of his parents he wants, and if he is a man he has the option of staying alone and being independent of both of them. But it is not recommended for him to stay away from both of them or to stop honouring them. If the child is a girl then she has no option of staying alone and her parents have the right to prevent her from doing that, because there is no guarantee that she will be safe from someone who will enter upon her and do her harm and bring shame upon her and her family; if she has no father than her guardian and family have the right to prevent her from doing that.
End quote from al-Mughni, 8/191
And Allah knows best.