RIO DE JANEIRO — Brazil’s top electoral court cleared President Michel Temer on Friday night of claims that he violated campaign finance laws, lifting a critical burden on the deeply unpopular leader as he resists calls to resign over a simmering graft scandal.

Judges on the Superior Electoral Court, which oversees federal elections, ruled 4 to 3 in Mr. Temer’s favor in a case that would have opened the way for him to be unseated as president if he had lost. The court also opted to significantly narrow the case’s scope to exclude a trove of testimony detailing how Odebrecht, a Brazilian construction giant, illegally financed the 2014 ticket on which Mr. Temer was the running mate of Dilma Rousseff, the former president.

Mr. Temer, 76, a centrist who has drifted to the right, rose to power just a year ago after Congress removed Ms. Rousseff, a leftist and the first woman to become president in Brazil, over claims that she manipulated the budget to conceal economic problems. Though they are now adversaries, both Mr. Temer and Ms. Rousseff insisted that they had not taken illegal campaign donations. Judges on the electoral court who are largely supportive of Mr. Temer or were appointed by him argued to keep him in office.

With Mr. Temer’s approval ratings falling into the single digits as he scrambles to quell protests on the streets of Brazilian cities, the electoral court’s maneuvering is igniting a debate over the functioning of the country’s branches of government, with corruption scandals hitting not just the president but members of his cabinet, the leaders of Congress and prominent members of the judiciary.