WASHINGTON—Federal judges have no authority to correct partisan gerrymandering, the Supreme Court ruled Thursday in a 5-to-4 decision that allows politicians to keep drawing electoral districts that entrench their power unless state law or Congress prevents them from doing so.

The decision by the court’s conservative majority overruled lower courts and rejected two constitutional challenges to partisan district mapmaking, one brought by Democrats in North Carolina and another by Republicans in Maryland.

“Excessive...