The laws on custody of children apply to both married and unmarried parents. For unmarried parents, paternity must be established before custody can be ordered. See Questions 23 through 27 on this web site for information about establishing paternity.
Legal custody means having the legal responsibility for caring for a child.
The divorce judgment or court order will usually say who gets custody. If the parents agree between themselves on custody, they can avoid a long and expensive court case. But if they can't agree, the judge will hear both sides and decide what's best for the child, not the parents. Judges will often award permanent legal custody to the parent who has had physical custody of the child. Judges do not like to change the living situation of a child who is doing well.