Custody can be determined by the parents themselves in non-aggressive or non-acrimonious divorces or separations free of abuse or other aggravating factors, or by the courts themselves in cases where neither parent can mutually agree to terms beforehand.
If a custody case proceeds to court, the judge will consider things such as the child's welfare and best interests. The judge will look at the health and habits of both parents, their ability and history of being a primary caregiver, their living arrangements (new romantic partners, suitable quarters for a child, environmental concerns), and so on. The wishes of the child may also be given some weight, depending on the age of the child and the circumstances surrounding the case in question.