A child’s wish to bear the name of the man who lives with and cares for her became a decisive factor in an unusual intervention by the Supreme Court