The court in an opinion authored by Justice Brett Kavanaugh concluded that the laws do not violate either the 14th Amendment to the Constitution, which requires that the law apply evenly to everyone, or Title IX of the Education Amendments of 1972, which bars sex discrimination in education.
Although the ruling directly concerns only West Virginia and Idaho, it is likely to affect 25 other states with similar bans.
It is the latest in a string of defeats for transgender people at the Supreme Court, which has a 6-3 conservative majority.