Brazil's ex-President Luiz Inácio Lula da Silva will be jailed while he appeals against a graft conviction, the Supreme Court has ruled.
He is facing 12 years in jail on charges of accepting a bribe but had asked to remain free during his appeal.
Lula claims the charges are politically motivated, and designed to prevent him from running for president in October.
He has a lead in opinion polls. His Workers' Party said the ruling was a "tragic day for democracy and Brazil".
The Supreme Court judges ruled against him by six to five after a marathon session, which ended in the early hours of Thursday.
Lula watched the ruling at the Metalworker's Union, where his supporters held an upbeat concert. The 72-year-old former president is likely to remain free until paperwork for his arrest is completed.
Lula served as president between 2003 and 2011. Despite his lead in the polls, he remains a divisive figure.
Up to 20,000 people protested in São Paulo on Tuesday calling for his immediate imprisonment, while supporters also rallied in large numbers in a rival demonstration.
Agony and ecstasy - Katy Watson, BBC News, São Paulo
While Brazilians are used to frequent delays and rambling verdicts in their courts, this 10-hour deliberation felt particularly long in this highly charged atmosphere.
Lula's legal battles have divided Brazilians and this decision was no different. His critics launched fireworks in celebration. Lula's supporters went home angry about what they say is an affront to democracy and a coup.
Lula's critics though see this as a victory. He's a politician who's been proven to be corrupt at the highest level, they say, and deserves to be behind bars - within days that could become a reality, crushing his hopes of returning to power.
This has been a massive fall from grace for a man who was once one of the world's most loved politicians. It was a historic day for Brazilian politics too.
What was the ruling about?
Until recently, defendants in Brazil were allowed to remain free until their final appeal had been exhausted.
However, the Supreme Court was considering a 2016 ruling from a lower court, under which defendants could be sent to jail after a failed first appeal.
Lula lost his first appeal in January, when the appeals court not only upheld his conviction, but increased the sentence from nine and a half years to 12.
The charges came from an anti-corruption investigation known as Operation Car Wash, which has implicated top politicians from several parties.
Who is Lula?
A former metalworker and trade union activist, he was the first left-wing leader to make it to the presidency in Brazil in nearly half a century.
During his presidency, Brazil experienced its longest period of economic growth in three decades allowing his administration to spend lavishly on social programmes.
Tens of millions were lifted out of poverty thanks to the initiatives taken by his government and he left office after two consecutive terms (the maximum allowed in Brazil) with record popularity ratings.
What is Operation Car Wash?
In 2014, after Lula left office, prosecutors started investigating allegations that executives at the state oil company Petrobras had accepted bribes in return for awarding contracts to construction firms.
The investigation, dubbed Operation Car Wash, uncovered a huge web of corruption involving top-level politicians from a broad spectrum of parties taking kickbacks.
Lula himself was convicted of receiving a renovated beachfront apartment worth some 3.7 million reais ($1.1m, £790,000), as a bribe by engineering firm OAS.
The defence says Lula's ownership of the apartment has never been proven and that his conviction rests largely on the word of the former chairman of OAS, himself convicted of corruption.
What does Lula say?
Lula has described the battle against his conviction and prison term as a continuation of his fight against Brazil's military rule, which came to an end in 1985.
"I did not accept the military dictatorship and I will not accept this dictatorship of the prosecutors," he told a gathering of supporters on Monday.
What happens next?
The judge in charge of Operation Car Wash, Sérgio Moro, is expected to issue a warrant for Lula's arrest within days.
But this does not necessarily mean that Lula will go to jail for 12 years.
He has not exhausted his appeals yet. There are two higher courts which he can still turn to, the Superior Court and the Supreme Court - the latter has only ruled for far on whether he should go to jail pending further appeals, rather than on the underlying case.
Neither of those courts would re-examine whether Lula was guilty of corruption. They would look into whether legal procedures were followed correctly and whether his constitutional rights were breached.
This process could take months or even years. If either court were to rule in Lula's favour, his conviction could be annulled and he would be released.
What about the election?
Under a 2010 Brazilian law called "clean slate law", no one convicted of a crime upheld on appeal can run for elected office for at least eight years.
That law would rule Lula out from running for the presidency in October. However, exceptions have been made to the law before.
The decision as to whether Lula can stand for president will rest with the Superior Electoral Court (TSE).
The TSE will not make a decision on whether Lula can run or not until he has registered as a candidate and he has until 15 August to do so. So we may not know for months to come whether he will be able to stand.