The Law Lords said on Thursday that such detention - on suspicion alone - contravened democratic rights and international obligations under the European Convention on Human Rights.
"The measures are unjustifiably discriminatory against foreign nationals and are not strictly required as they require the detentions of some, but not all, those who present a risk," said Lord Thomas Bingham, head of the nine-judge panel.
Another of the nine, Lord Leonard Hoffmann said that in his opinion: "There are no adequate grounds for abolishing or suspending the right not to be imprisoned without trial, which all inhabitants of this country have enjoyed for more than three centuries".
The lords ruled 8-1 against the detentions.
After a series of earlier legal challenges, the latest case was heard before the Law Lords, a group of senior judges who serve in Britain's upper chamber of parliament, the House of Lords, and act as the country's highest legal power.
The ruling is a setback for Blair's
Due to the constitutional importance of the challenge, the decision was made by a court of nine Law Lords, rather than the usual five.
Attorney-General Lord Peter Goldsmith has argued detention without trial is the only way to deal with foreign suspects who will not leave voluntarily but cannot be deported because they face death or mistreatment at home.
Setback for Blair
The ruling is a serious blow to Prime Minister Tony Blair, coming just hours after Home Secretary David Blunkett - architect of the controversial measure - resigned following a personal scandal.
The foreigners, mainly held at the high-security Belmarsh Prison in southeast London, labelled "Britain's Guantanamo Bay" by human rights groups, are being held under the Anti-Terrorism, Crime and Security Act.
Introduced in the wake of the 11 September 2001 attacks on the US, the law allows foreigners to be jailed indefinitely without charge or trial if the home secretary rules they are suspected of involvement in international terrorism and they opt not to leave the country.
According to advocacy groups, a total of 14 people are being held under the law either in prison or secured hospital wards, some detained for up to three years.
The current case is dealing with only nine of these.