The justices ruled that the state’s law, which prohibited some medical treatments for transgender youths, did not violate equal protection principles.