WASHINGTON  Elected judges must disqualify themselves from cases involving people who spent exceptionally large sums to put them on the bench, the Supreme Court ruled on Monday in a 5-to-4 decision.

The decision, the first to say the Constitution’s due process clause has a role to play in policing the role of money in judicial elections, ordered the chief justice of the West Virginia Supreme Court to recuse himself from a $50 million case against a coal company whose chief executive had spent $3 million to elect him.

Thirty-nine states, including New York, elect at least some of their judges, and election campaigns, particularly for state supreme courts, have in recent years grown increasingly expensive and nasty. In the last decade, spending on elections for state high courts has reached $200 million, according to Justice at Stake, a group that tracks campaign spending. Elected judges routinely accept contributions from lawyers and litigants who appear before them, and they seldom disqualify themselves for cases involving donors.

Justice Anthony M. Kennedy, writing for the majority in a decision that split along familiar ideological lines, said the Constitution required disqualification when an interested party’s spending had a “disproportionate influence” in a case that was “pending or imminent.”