Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

RIO DE JANEIRO — Brazil's top court voted narrowly to turn down an attempt by former President Luiz Inacio Lula da Silva to stay out of jail while he appeals a corruption conviction, a decision that will have widespread implications in this polarized nation.

After nearly 11 hours of often heated debate, the justices of the Supreme Federal Tribunal voted 6-5 early Thursday to deny Lula's request to stave off a 12-year jail sentence while he fights a conviction in a case that he argues was nothing more than a ploy to keep him off of October's presidential ballot.

Despite the conviction and several other corruption charges pending against him, Lula leads all preference polls for the election. The decision means he will likely be jailed soon, though probably not until at least next week thanks to various technicalities.

Within minutes of the decision, Lula's Workers' Party, which held Brazil's presidency from 2003 to 2016, put out a tweet that foreshadowed the struggles to come.

The Morning Rundown Get a head start on the morning's top stories. This site is protected by recaptcha

"The Brazilian people have the right to vote for Lula, the candidate of hope," it read. "The Workers' Party will defend this candidacy on the streets and in every court until the last consequences."

The court's debate underscored how fraught the matter is at a time of high tension and angst in the country, which is struggling to emerge from a crippling recession and is four years into a major corruption scandal that has ensnared much of the country's elite, including Lula.

Brazilian society remains deeply divided after Lula's successor, President Dilma Rousseff, was impeached and removed from office amid the crises.

"The constitution secures individual rights, which are fundamental to democracy, but it also assures the exercise of criminal law," said Chief Justice Carmen Lucia, who cast the deciding vote after spending much of the session being criticized by colleagues.

Justice Gilmar Mendes, traditionally a critic of Lula, voted in favor of the former leader's petition to stay out of jail, challenging his colleagues to buck pressure from society.

"If a court bows (to pressure), it might as well not exist," said Mendes.

The heated session reflected the debate happening across Brazil as millions tuned into the televised session. When the decision was leveled, fireworks and yells could be heard and seen in Rio de Janeiro and Sao Paulo, two of the nation's most important cities.

Lula was once wildly popular after his two terms as president from 2003 to 2010, but he has become a polarizing figure amid the "Car Wash" corruption scandal that has roiled Brazil the last several years and made average citizens furious with the political class.

Lula was convicted in July of helping a construction company get sweetheart contracts in exchange for the promise of a beachfront apartment. He denies any wrongdoing in that case or in several other corruption cases that have yet to be tried. An appeals court upheld the conviction in January, and the three reviewing magistrates even lengthened the sentence to 12 years and one month.

Technically, the Supreme Federal Tribunal's decision doesn't keep Lula off the ballot. The country's top electoral court makes final decisions about candidacies beginning in August, but it has been expected to deny his candidacy under Brazil's "clean slate" law, which disqualifies people who have had criminal convictions upheld.