The group have been held in the detention centre since late 2001
Five Algerians held at Guantanamo Bay without charge for almost seven years must be freed, a US judge has ruled.
Judge Richard Leon said the government had failed to prove they had planned to go to Afghanistan to fight US troops.
The men were arrested in Bosnia shortly after the 9/11 attacks and have been held without charge ever since.
They are the first group of inmates to challenge their detention in civilian courts since the US Supreme Court ruled that they could in June.
The men were detained by Bosnian authorities in October 2001 and sent to Guantanamo Bay in January 2002.
They were initially accused of plotting to bomb the US embassy in Sarajevo, but these allegations were dropped.
US authorities also accused the men of planning to go to Afghanistan to link up with Taleban and al-Qaeda fighters.
But Judge Leon said that the government had "failed to show by burden of proof" that this was the case.
A sixth Algerian, Belkacem Bensayah, who was detained along with the group, should remain in custody, he ruled.
The government had established he was "more likely than not" planning to go to Afghanistan, he said.
The judge called on the US government to take all necessary steps to allow the release of the five men "forthwith".
But, says the BBC's Kim Ghattas in Washington, that does not mean that the men will be freed very promptly.
The US government has the right to appeal against the ruling.
The White House said it disagreed with the judgement and that the justice department was reviewing it.
"This ruling does demonstrate the need for Congress to enact procedures that allow these petitions to be adjudicated in a way that is fair to the detainee but that allows the government to present its case without imperilling national security," said White House spokesman Tony Fratto.
About 250 detainees accused of terrorism or links to al-Qaeda remain in the controversial detention camp in Cuba. Many of them are now in the process of challenging their detention.
US President-elect Barack Obama has promised to close down Guantanamo Bay once he takes office in January.