How does the judge decide who should have custody of the children in a divorce?

In Michigan custody is decided based on the best interests of the child or children. There are twelve "best interests" factors in the Michigan Compiled Laws that outline the things a court must consider when deciding custody. Of course, parents can always reach an agreement on custody and parenting time; the court will almost always honor such an agreement.