SÃO PAULO—Brazil’s Supreme Court rejected former President Luiz Inácio Lula da Silva’s efforts to avoid jail in a watershed ruling early Thursday that is expected to land the populist leader behind bars within days and end his bid for re-election.

In a case that has divided this nation and put the country’s three-decade-old democracy to the test, the court’s justices voted 6-5 against the leftist icon, denying his request to remain out of prison while he exhausts all possible appeals against his conviction for corruption.

The 72-year-old was found guilty last year of accepting a penthouse apartment from a construction firm in exchange for favors, landing him a 12-year prison sentence and marking a dramatic fall from grace for the former shoeshine boy who left office seven years ago as one of the world’s most popular politicians.

Thursday’s ruling all but ends Mr. da Silva’s hopes of running in October’s presidential election, a vote polls show he would win. The outcome also propels other potential candidates to the front of the pack, including a right-wing former army captain, Jair Bolsonaro, and the environmentalist Marina Silva.

Brazil’s streets were largely deserted initially after the ruling early Thursday, which came at around 12:30 a.m. local time after almost 11 hours of deliberations. But authorities were bracing for demonstrations later Thursday.