Radical cleric Abu Qatada is to be released on bail weeks after the European Court of Human Rights blocked his deportation to Jordan.
Mr Justice Mitting told the UK's Special Immigration Appeals Commission the cleric should be freed within days.
The Home Office said it disagreed with the decision and the cleric continued to be a threat to national security.
The government opposed attempts to bail him while he fights deportation to Jordan - where he faces terror charges.
The European Court of Human Rights says if he were deported, then Jordan might use evidence obtained by torture.
Lawyers for the preacher told the Special Immigration Appeals Commission on Monday that his continued detention was unreasonable.
Ed Fitzgerald QC said: "There comes a time when it's just too long, however grave the risks."
Mr Justice Mitting said although the cleric had been detained for a long time pending deportation this was not illegal.
But he said the preacher should now be bailed because his deportation had been blocked by the European Court.
The UK government wanted the cleric to stay in jail while diplomats try to strike a new deal with Jordan which would satisfy the European Court that the 51-year-old would not be tried on evidence obtained from torture.
The judge gave Home Secretary Theresa May three months to show that British diplomats had made progress in these negotiations or else see Abu Qatada's stringent bail conditions revoked.
Abu Qatada's lawyer Mr Fitzgerald said earlier his client had been held for six-and-a-half years while fighting deportation "against a background of almost nine years' detention without charges on the grounds of national security".
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".
Mr Justice Mitting set similar bail conditions to those the cleric had in 2008 because, he said, Abu Qatada "has shown no inclination of any change in attitude" since that time.
The cleric will be released in the coming days from Long Lartin prison, in Worcestershire, and taken to an address in London - which will have been pre-assessed by MI5.
Mr Justice Mitting said Abu Qatada would only be allowed to leave the address for two one-hour periods a day.
Anyone visiting his home, bar his wife and children, have to be pre-approved and he must give notice of all meetings arranged outside his home.
He will not be allowed to leave the general area and will also have no access to the internet or electronic communications devices.
A Home Office spokesman said: "Qatada should remain in detention, our view has not changed. That is the argument we made to the court today, and we disagree with its decision.
"This is a dangerous man who we believe poses a real threat to our security and who has not changed in his views or attitude to the UK.
"We have argued for the strictest possible bail conditions to be imposed on Abu Qatada, because this government will take all necessary measures to protect national security.
"This is not the end of the road and we are continuing to consider our legal options in response to the European Court's ruling."
Labour MP Keith Vaz, who is also the chairman of the Commons Home Affairs Select Committee, said he was astonished and the public would consider the ruling to be "pretty extraordinary for someone who has been implicated in a number of very dangerous situations".
But Roger Smith, director of the law reform group, Justice, said the judge's decision was the right one.
He said: "The problem is, no-one has ever accused [Qatada] specifically of any offence in the United Kingdom. Presumably the security services know more - well, prosecute him."
In its judgement in January, the Strasbourg judges said the UK had struck a deal with Jordan to guarantee the alleged extremist's human rights - but the cleric could still be the victim of injustice if deported.
He has been convicted in his absence of terrorism offences and is likely to face trial if returned. He alleges the co-defendants in the cases implicated him after they were tortured - meaning he could not face a fair trial.
The cleric has been imprisoned or held on strict curfew conditions for six and a half years.
Abu Qatada had been detained for two years and 10 months in prison, followed by six months on curfew - and then another three years and two months back in jail.