Home Secretary Theresa May has failed in a bid to deport a 33-year-old Ethiopian who had "thrown in his lot" with Islamist extremists "committed to terrorism".
The Court of Appeal said there was a risk that the man - who was not named in a ruling - might be subjected to "ill treatment" if returned to Ethiopia.
Three appeal judges overturned a ruling by the Special Immigration Appeals Commission (Siac) - which had upheld Mrs May's decision to deport the man, who has a wife and children in the UK.
They said Siac had "erred" in ruling that assurances given by the Ethiopian government were "sufficient" to prevent the man being "subjected to ill treatment" contrary to human rights legislation.
Lord Justice Jackson - the most senior of the three appeal judges - said he had reached his decision with "little enthusiasm".
He said he had "no doubt" that Mrs May had been entitled to conclude that the man's deportation was "conducive to the public interest on national security grounds".
But he said "everyone within our shores" is entitled to protection under human rights legislation - "even those" involved or connected with terrorism.
The man - referred to as J1 in a written judgment handed down today after an appeal court hearing in London in February - had appealed against Siac's decision to uphold Mrs May's decision to deport him to Ethiopia.
He argued that Siac erred in holding that assurances given by the Ethiopian government were sufficient to prevent him from being subjected to ill treatment contrary to article 3 of the European Convention on Human Rights (ECHR), which prohibits torture and inhuman or degrading treatment or punishment.
The three appeal judges - Lord Justice Jackson, Lord Justice Elias and Lord Justice Treacy - agreed.
"The appellant succeeds in this appeal and the decision of Siac dismissing the appeal is reversed," said Lord Justice Jackson.
"I should add that this is a decision which I reach with little enthusiasm. The appellant has been closely associating with Islamist extremists who are involved in terrorism. Such people show scant regard for the rights to life of others.
"I have no doubt that the Secretary of State was entitled to conclude that the appellant's deportation was conducive to the public interest on national security grounds. The fact remains, however, that the UK is party to the ECHR and has incorporated its provisions into our domestic law.
"Everyone within our shores is entitled to protection under the ECHR, even those who are involved in or connected with terrorism. The courts are under a duty to uphold those rights and we do so in this case."
Lord Justice Jackson said the man arrived in the UK with members of his family in 1990.
"Most of the family returned to Ethiopia in 1992, but the appellant and his sister remained in this country. Eventually, after various refusals of asylum and appeals, the appellant obtained indefinite leave to remain in the UK. He now has a wife and children in this country," said the judge.
"Unfortunately the appellant has not lived peacefully in his host country. Instead he has thrown in his lot with a group of Islamist extremists who are committed to terrorism."
Lord Justice Jackson said there were "hostile organisations" in Africa.
"The branch of Al Qaida which operates in the Horn of Africa is known as Al Qaida in East Africa (AQEA)," said the judge.
"Another Islamist extremist organisation in Africa is known as Al Shabaab. Al Shabaab has declared the ambition of establishing a caliphate in the Horn of Africa. The Ethiopian parliament has declared AQEA and Al Shabaab as terrorist organisations."
Lord Justice Jackson gave a list of the man's associates and outlined a series of terrorism connections, including links to a failed London bomb plot in 2005 and training camps in Cumbria and Scotland.
He said the man was "an important and significant member of a group of Islamist extremists in the UK".
"The appellant has been associating with the following men: Dawit Semeneh, Joseph Kebide, Nathan Oqubay, Zulgai Popal, Elias Girma Eyassu, Bilal Berjawi, Mohammed Sakr, and Walla Eldin Rahman," Lord Justice Jackson.
"In May 2004 Semeneh and Kebide attended a training camp in Cumbria which was run by a man called Hamid. Hamid was subsequently convicted of soliciting to murder and providing terrorism training.
"The training camp in May 2004 appears to have been a serious affair. Four men who were involved in the failed London bombings of 21st July 2005 attended Hamid's training camp in May 2004.
"In December 2004 the appellant and three others were found by the police at Lanark. They were all wearing plastic gloves. They said that they were on their way to Fort William. At about this time Hamid was organising a training or bonding camp near Loch Ness.
"In May 2005 Semeneh, Oqubay, Popal, Eyassu and Kebide travelled to Somalia for purposes relating to terrorism. The appellant admits that he knew about this trip but claims he thought it was for religious purposes.
"On 21st July 2005 the appellant was in telephone contact with a man called Hussain Osman. Osman was one of five men who attempted to cause bomb explosions in London on that day. He is now serving a substantial prison sentence."
Lord Justice Jackson added: "In October 2009 Berjawi, Sakr and Rahman travelled from the UK to Somalia for the purpose of terrorist training and terrorist activity in Somalia.
"The appellant knew in advance about the travel plans of those three men and the purpose of their expedition.
"During 2009 and 2010 the appellant was an important and significant member of a group of Islamist extremists in the UK who provided support to those three men in Somalia.
"Media reporting identifies Berjawi as a close associate of Harum Fazul. Until his death in June 2011 Fazul was a, if not the, leader of AQEA. Fazul was also a close associate of the leaders of Al Shabaab. On 11th July 2010 there were twin bombings in Kampala, for which Al Shabaab claimed responsibility. It is now generally thought that that claim is well founded.
"Perhaps unsurprisingly the Secretary of State came to the conclusion that the deportation of the appellant from the UK would be conducive to the public good for reasons of national security.
"Accordingly on 25th September 2010 the Secretary of State notified the appellant of her decision to make an order that the appellant be deported to Ethiopia. The appellant was taken into immigration detention on 25th September 2010."