How Do the Courts Determine Who Gets Custody?

How Do the Courts Determine Who Gets Custody?