The mother has more right to her children - if they are below the age of discernment - that her husband, so long as she does not get married. If she gets married then the children go back to their father.
Once the children reach the age of discernment, they should be given the choice between their parents.
This applies if the father and mother are equal in terms of religious commitment and good character. If one of them is of bad character and not very religiously committed, he or she should not be given the right of custody, because what counts is what is in the best interests of the child, and his interests cannot be served with one who will neglect the child's rights. This is what is indicated by the hadeeths and this is what was mentioned in fatwas by the most prominent scholars.
It was narrated from Abd-Allah ibn Amr (may Allah be pleased with him) that the Prophet (blessings and peace of Allah be upon him) said to a woman who sought custody of her child from her husband: You are more entitled to him so long as you do not get married. Narrated by Abu Dawood in his Sunan (2276); classed as hasan by Shaykh al-Albaani in al-Silsilah al-Saheehah (368).
Ibn al-Qayyim (may Allah have mercy on him) said:
I heard our shaykh, may Allah have mercy on him - meaning, Ibn Taymiyah - say:
Two parents referred a dispute concerning a child to one of the judges, who gave him the choice between his parents and he chose his father. His mother said to him: Ask him why he chose his father. So he asked him and he said: My mother sends me every day to the school and the teacher hits me, but my father lets me play with other children. So the judge ruled that he should stay with his mother and said: You have more right to him.
Our shaykh said: If one of the parents fails to teach the child and instruct him in what Allah has enjoined upon him, then he is a sinner and he has no right to guardianship over him. The one who does not fulfil the duties of his guardianship has no right to guardianship. He should be dismissed and another person should be given guardianship, who will do what is required, or he should be put with someone else who will do what is required. The aim is obedience to Allah and His Messenger only.
And he said: If it so happened that the father marries a woman who does not take care of his daughter's interests or look after her, and her mother will take better care of her than this wife, then custody in this case should definitely go to the mother.
And he said: It should be noted that there was no general text from the Lawgiver giving precedence to either of the parents in absolute terms, and the child should not be given the choice between his parents in all cases. The scholars are unanimously agreed that neither of them should be singled out in all cases; rather one who is an evildoer and heedless should not be given precedence over one who is righteous and of good character.
And Allah knows best.
Zaad al-Maaad, 5/475, 476
Shaykh Muhammad ibn Ibraaheem (may Allah have mercy on him) said:
The custody of the young girl goes to her mother so long as she does not get married or until the girl reaches the age of seven years, when custody passes to her father so long as she will not be harmed by staying with her father.
In the case of an older girl, custody goes to her father so long as she will not be harmed by staying with her father's new wife.
Fataawa al-Mar'ah al-Muslimah, 2/874
And Allah knows best.
Reproduced from Islam QA