Child custody cases are uncomfortable for everyone. Parents are adversaries, the child is caught in the middle and the judge has to make a decision that won't make anyone completely happy. These factors generally influence that decision.
The "best interests" of the child, including personal safety, psychological welfare and cultural familiarity and educational opportunity, is always the primary consideration in decisions affecting custody and visitation.
Another primary factor is which parent acts--and will have the ability to act in future--as caretaker in everyday family, educational and recreational activities.
Most states make provision for the child's wishes above a certain age, most often somewhere from 10 to 14 years old. Judges take into consideration the child's maturity, its best interest and any criminal record of parents.
Parties to custody cases often bring informal agreements or settlements negotiated out of court that are often accepted if they are in the best interests of the child.
In most states, the mother is considered the primary caregiver if parents are not married and is generally awarded custody unless some other consideration is present.
Third-party petitions by parties other than parents might be considered by the court. State laws vary widely concerning factors such as religion and alternative lifestyles, but consideration of these factors is always secondary to the best interests of the child.