SÃO PAULO, Brazil — Brazil’s top electoral court ruled on Friday that former President Luiz Inácio Lula da Silva, who is serving a 12-year sentence for corruption, cannot run for a third term, eliminating the front-runner in a race many see as a struggle between democracy and due process.

Mr. da Silva, a two-time president who left office in 2011 with a record-high approval rating, has become a polarizing figure. Many supporters remain loyal to the metalworker who rose from the factory floor to the presidency and improved the lives of millions of the country’s poorest, giving him a wide lead in polls ahead of the Oct. 7 vote.

But he also has one of the highest rejection rates thanks to the Lava Jato, or Carwash, investigation that landed Mr. da Silva in jail and documented the political corruption that flourished during the 13 years that his party was in power.

In a poll published in August by the Datafolha research institution, Mr. da Silva easily led his rivals, with 39 percent of Brazilians saying they would vote for him, followed by a far-right former army captain, Jair Bolsonaro, who had the support of 19 percent of expected voters. His ouster blows open an already splintered race, just five weeks before the vote.