Radical cleric Abu Qatada has won his appeal against deportation from the UK to Jordan, at the European Court of Human Rights.
The judges accepted the UK's deal with Jordan to protect the cleric from abuse was sound.
But the court said he should not face trial for terrorism on evidence obtained by the torture of others.
Home Secretary Theresa May said the European judges' decision was "not the end of the road".
The British government can make a final appeal before the judgement becomes binding in three months' time. If it does not appeal, the cleric will have to be released from detention.
Abu Qatada, whose real name is Omar Othman, is one of the most influential Islamist clerics in Europe, supporting jihadist causes. British judges have described him as "truly dangerous".
He has never faced trial in the UK, but has been detained without charge and had his movements restricted by a control order, a form of house arrest.
The Palestinian-Jordanian preacher has been convicted in his absence of involvement in two major terrorism plots in Jordan.
But he says that those convictions were based on evidence extracted by the torture of co-defendants and he would face similar treatment if returned. He originally fled to the UK in 1993 after being tortured twice.
The government signed a memorandum of understanding with Jordan as part of its efforts to expel him, one of a number of deals with foreign regimes which are designed to protect the human rights of anyone deported from the UK.
In the ruling, the Strasbourg court accepted that diplomatic assurances given by Jordan to the UK meant that the cleric would be protected from torture if he were returned.
But it added that the deportation had to be stopped because Abu Qatada was likely to face retrial in Jordan - and that torture had been used to gather the evidence against him.
"Torture and the use of torture evidence were banned under international law," said the European Court.
"Allowing a criminal court to rely on torture evidence would legitimise the torture of witnesses and suspects pre-trial. Moreover, torture evidence was unreliable, because a person being tortured would say anything to make it stop.
"The Court found that torture was widespread in Jordan, as was the use of torture evidence by the Jordanian courts.
The Court also found that, in relation to each of the two terrorist conspiracies charged against Mr Othman, the evidence of his involvement had been obtained by torturing one of his co-defendants.
"When those two co-defendants stood trial, the Jordanian courts had not taken any action in relation to their complaints of torture."
Strasbourg said that it was highly probable that the "decisive" evidence against the preacher had come from abusing these defendants.
"In the absence of any assurance by Jordan that the torture evidence would not be used against Mr Othman, the Court therefore concluded that his deportation to Jordan to be retried would give rise to a flagrant denial of justice in violation of [his right to a fair trial]".
Gareth Peirce, Abu Qatada's lawyer, said: "It has always been astonishing that this country has fought long and hard to assert that a violation of such a fundamental legal principle should take place.
"What message would it have sent to the rest of the world if the European Court of Human Rights had held that it was acceptable to send a civilian to be tried in a military court on evidence emanating from torture?"
But in a statement, Home Secretary Theresa May said she was disappointed by the ruling.
"This is not the end of the road, and we will now consider all the legal options available to us," said Mrs May. "In the meantime, Qatada will remain in detention in the UK. It is important to note that this ruling does not prevent us seeking to deport other foreign nationals."
Julia Hall of Amnesty International welcomed the decision not to deport but added: "This positive development is eclipsed by the court's conclusion that diplomatic assurances can, under certain circumstances, be sufficient to reduce the risk of torture.
"This is an alarming setback for human rights. Diplomatic assurances are no substitute for respect for the legal obligation not to send a person to a place where they are at real risk of torture."
But Blair Gibbs of think tank Policy Exchange, said: "This is a flawed ruling that drastically raises the bar for deportation cases.
"We urgently need a new human rights settlement because it is our own courts, and not Strasbourg, who are best qualified to balance the rights of defendants against wider national security."
Shami Chakrabarti, Director of human rights campaign group Liberty, said: "Today's decisions from the European Court of Human Rights show considerable trust in the UK legal system but understandable concern about Jordanian law.
"The court found that torture and evidence obtained that way is widespread in that country. So it is clear that if Abu Qatada is to be tried for terrorism, this should happen in a British court without further delay."