At the end of a lengthy day in court at Guantánamo Bay, the five defendants take out small, woven mats and, turning away from the judge, kneel down to face in the direction of Mecca and pray. With US military officers and their lawyers, arms casually folded, watching patiently, it is a civil and even disarming scene.

At the head of the prisoners is Khaled Sheikh Mohammed, the al-Qaeda operative and self-proclaimed mastermind of the 9/11 attacks. His beard, a dark black when he was captured in Pakistan in 2003, is now a henna red. He is wearing a combat jacket, a signal that if the US contends it is still at war with him, he wants it known he remains a willing warrior himself after a decade in captivity.

By the time Mohammed stops to pray, all the journalists have been cleared out of the expansive hearing room, where they usually sit behind a glass screen, listening to proceedings on a 40-second delay. I see the scene unfolding only by chance on a closed circuit television in the shed next to the courthouse, where I have been taken to register for my security badge.

Behind Mohammed in the tailor-built courtroom are his co-defendants, dressed in a mix of traditional Middle Eastern tunics and desert military outfits: two Yemenis, a Saudi and another Pakistani. They have been held by the US for more than a decade, for most of it at Guantánamo Bay and, in some cases, for a period at CIA black sites around the world, where they were interrogated.

Their prayers over, the detainees seem surprisingly relaxed. They gather their belongings as if finishing a day at the office, chat to their lawyers and guards, pack their papers and prepare to be secured and driven back to their cells. Their heavily guarded convoy is one of the few things that causes traffic jams on the small US naval base, covering just 120 square kilometres of water and land.

More than 12 years after the detention centre was established, the US base at Guantánamo Bay has only 149 detainees left, down from a maximum of just under 800 in its early days. Of those, more than half – 78 – have been cleared for release as low-risk detainees but have yet to be repatriated or resettled, either because countries can’t be found for them or because Barack Obama hasn’t been willing to take the risk of sending them to countries such as Yemen, where they could join terror groups. Of the remainder, only about 20 have been charged or will ever face charges, according to US military officials. The other 50-odd detainees remain in legal limbo, behind bars indefinitely but charged with no crimes. The oldest detainee is now 67.

Guantánamo Bay was picked because some in the Bush administration thought it was the perfect law-free zone

The ambiguous status of Guantánamo Bay, a US naval base leased from Cuba in perpetuity in 1903, is what made it so attractive to George W Bush when he pondered what to do with prisoners plucked off the battlefield, largely from Afghanistan, after 9/11.

One of his advisers figured the island was the “legal equivalent of outer space”, according to a recent book on Guantánamo, The Terror Courts, by Jess Bravin. In other words, it was under US control but, the Bush White House hoped, beyond the reach of its lawyers and courts. “Guantánamo Bay was clearly picked because some in the Bush administration thought it was the perfect law-free zone,” says Morris Davis, a former chief prosecutor for the military commissions established to try the prisoners. “Otherwise, why would they fly someone 7,000 miles around the world to get them there?”

But US law, and lawyers, have gradually intruded on the military commissions created to try the detainees in the years since. Far from being an expeditious way to try, convict and, in some cases, execute men the US detained in the all-encompassing “war on terror”, Gitmo has drifted into a legal tangle of Dickensian dimensions.

The five accused 9/11 conspirators attend a pre-trial hearing in Guantánamo Bay naval base, June 16 2014 (Photograph: Reuters)

On its own, the death penalty trial of the alleged 9/11 conspirators is unfolding as one of the most complex, contentious and lengthy legal cases in recent US history, and one which may set new standards for war crimes hearings around the world. But the fierce tussle over whether Guantánamo’s military commissions – legal proceedings with most but not all of the protections of ordinary courts – can deliver a fair trial is only one aspect. The real and largely unstated battle revolves around Guantánamo’s original sin – the use of torture by the US government to gather information from many of the men held and being tried there. The application of torture, or “enhanced interrogation techniques” in the US government’s euphemism, has been well documented by human rights groups, in official government documents and even in the memoirs of former CIA officers.

“Why are we trying this case here instead of in one of hundreds of US courts? Because highly placed people in the US government permitted or carried out torture and, in the process, committed 20-year felony crimes,” says David Nevin, Mohammed’s lawyer.

The intelligence subterfuge that has characterised Guantánamo since its inception has dogged the 9/11 commissions as well. Over the past 18 months, bugs have been found in the ceiling smoke detectors in the rooms defence lawyers used to talk with their clients, and lawyers’ emails were accessed. Without recourse to the judge, the CIA cut the public feed of the hearings, by activating a red light which resembles a police siren in the court, just as Nevin was asking for information about his client. In April this year, the lawyers discovered the FBI had questioned and tried to recruit as an informant a security expert working for the defence team to discover how Mohammed’s personal writings had been published in The Huffington Post, delaying the formal trial further. “They put a plant into the defence team – I mean, what?!” says Nevin.

The battle over Guantánamo has already spanned two presidencies, and is set to land on the desk of a third after elections in November 2016. Even with convictions, the protracted political and legal entanglement over the detention facility, and the allegations of torture, may mean it is not closed until the likes of Khaled Sheikh Mohammed and his fellow defendants die a natural death.

…

Some of the first detainees, 2002 (Photograph: PA)

The first detainees arrived at Guantánamo in early 2002 and were held at an open-air prison once used to detain Haitian refugees known as Camp X-ray. Although the camp was closed after only a few months, the facility’s reputation has never really recovered from these first moments, captured in photos of the detainees, dressed in orange jumpsuits, hunched over on their knees and shackled in the open air, their mouths covered to stop them spitting at guards.

Preserved by court order, Camp X-ray today looks like an abandoned film set for a second world war prison-break movie. Rotting wooden guard towers overlook rows of two-metre by two-metre wire mesh cages used to incarcerate individual detainees. Up the hill, about 40 metres away, are the crumbling plywood interrogation facilities, with desks and chairs left just as they were, like a macabre museum exhibit. Clumps of grass and subtropical foliage are gradually creeping their way through the prison and turning it into a fetid, swampy ruin. The razor wire wrapped along the tops of multiple fences is tumbling down and a makeshift shower block is almost engulfed by new growth. Kenneth Edel, a US military officer, points out the block while conducting a journalists’ tour. “Armed doctrine,” he says, citing the reason for building a shower block near the camp’s entrance. “You make the prisoners clean up when they arrive.”

…

Cleaning up Guantánamo politically and legally has been much harder. Barack Obama bounded into office in early 2009 promising to close the facility, signing an executive order to shut it on his second day as president. Later that year, Eric Holder, his attorney-general, announced the alleged 9/11 conspirators would be tried in a federal court in New York. Congress, however, and an uproar in New York, blocked the way. Obama didn’t take up the issue again seriously until about a year ago, restarting the process to send some low-risk detainees home or to third countries, but there is little chance of the camp being emptied and closed by the time he steps down in early 2017.

President Obama swapped five Guantánamo detainees for Sgt Bowe Bergdahl (Photograph: AP)

The president got a taste of the problems in renewing repatriation of detainees in May, when he agreed to swap five senior Taliban members, held in Guantánamo without charge for more than a decade, for the last remaining American POW in Afghanistan, Sergeant Bowe Bergdahl. Congress growled as it was not consulted beforehand, and the conservative media erupted in fury. The latest outbreak of hostilities in Iraq and Syria, which could create a new anti-US battlefield, may make it more difficult for Obama to sell the case to send more detainees home.

With the collapse of efforts to bring them before courts on shore in the US, at least one issue has been settled, however. The 20-odd detainees who are, or who will go on trial in Guantánamo – the “worst of the worst”, as Dick Cheney, Bush’s vice-president, called them – will face the military commissions. They include the alleged 9/11 conspirators and the alleged bomber of the USS Cole in Yemen in 2000, Abd al-Rahmin al-Nashiri, a 49-year-old Saudi whose trial date has been set for December.

So far, the huge political and financial investment in the commissions has produced little return. After spending more than $5.2bn on the Guantánamo facility in the 12 years until the end of 2013 – at the cost of about $2.7m per annum per detainee, according to the Miami Herald – the commissions have secured a handful of convictions, most of them through plea bargains. The official $5bn-plus figure underestimates the cost of Gitmo, as it does not include the budget for the prison housing the “high-value” detainees such as the alleged 9/11 conspirators. Since it is funded out of the CIA budget, like so much that happens at the base, it is classified.

The fairness of the system is not necessarily what the defence counsel says it is

The laws governing the commissions have evolved in parallel with the changing political landscape in the US. The Supreme Court invalidated the Bush administration’s first commissions’ plan which afforded the detainees few rights. Congress later passed two laws – in 2006 and 2009 – each time giving greater protections for the defendants.

The commissions’ most articulate advocate is the chief prosecutor, Mark Martins. Appointed in 2011, the lanky brigadier general with a hawk-like demeanour has put his money where his mouth is, eschewing further promotions to commit himself to the job until late 2017. “Where crime leaves the beaten path, the law must be strong enough to follow,” Martins likes to say, quoting Robert Jackson, the chief US prosecutor at the Nuremberg war crimes trials.

Martins exudes resolute determination in patiently explaining the virtues of military commissions and their ability to deliver fair legal outcomes. He is not lacking in erudition. Top of his class at West Point, a Rhodes scholar at Oxford university and a Harvard Law School graduate, he distributes legal papers to buttress his arguments about how evidence gathered on the battlefield is fair game in military courts. “Anglosphere legal systems understandably have a great mistrust of hearsay from the Sir Walter Raleigh trial. Well, we are not falling into the Raleigh trial injustice here,” he says. (Raleigh was accused of treason in a celebrated trial in 1603 by attempting to make Arabella Stuart the Queen.) “The standard of the trial is governed by the Geneva conventions,” he says. “We are trying people in hostilities with us, that we are in armed conflict with, a group that killed more than 3,000 of our citizens and people from more than 90 countries. This idea of sham trials – professional military officers never bought into that. This is not our system.”

Brig Gen Mark Martins, chief prosecutor (Photograph: AFP)

Martins’ office sits in a building called the “Crow’s Nest” that overlooks the $12m courthouse built specially for the commissions in 2008 on an abandoned landing strip, lined with weeds growing through cracks in the concrete. When the court is in session about every six weeks or so, portable watch towers which squat on the four corners around the building unfold and rise up like Transformers characters to arch over the site. Snipers take positions on the surrounding roofs.

Guantánamo is already one of the most secure places on earth but nothing is left to chance. After the five 9/11 defendants are delivered to the court, each is placed in separate steel containers where they can meet their lawyers. They walk to the court through a wire mesh corridor, shaded for security with black sniper netting to conceal their presence, and lined with members of a protective “quick reaction force”. Before they enter the court and after they leave, the defendants sit on a chair screening them for metallic objects, either on them or inside them. The chair is called a Boss, which stands for Body Orifice Security Scanner. At every point in their journey – in their supermax prison, their holding cells at the court and in the court itself – are arrows pointing towards Mecca, should they want to pray at any time.

The defence lawyers have battled to establish a rapport with their clients. “One of the thing that torture does is to profoundly affect the ability to trust,” Nevin says of Mohammed. “I think he struggles with accepting the idea that a military court can deliver justice to someone who is said to be an enemy of the country.” Mohammed was waterboarded 183 times, according to a 2005 US Justice Department memo released in 2009, although Jose Rodriguez, who headed the CIA interrogation unit, reckons that was not what broke him in the end. In a book written after he left the agency, Rodriguez said keeping Mohammed awake for as long as 180 hours straight – and then asking him if he wanted a nap – did the trick.

David Nevin, defence lawyer for Khalid Sheikh Mohammed, addresses Judge James Pohl from the podium, June 16 2014 (Photograph: Reuters)

“It was the sleep deprivation that finally got him,” he said, adding in a later interview: “It was much kinder than anything he would have done to an American captive, like Danny Pearl.” (Mohammed admitted in custody to personally beheading the Wall Street Journal reporter in 2002 but he has not been charged with the crime.)

Often in trials, the prosecution tries to get evidence in and the defence tries to hide it, but this process has been reversed at Guantánamo. The defence is trying to find out what happened to their clients and the prosecution is blocking access. Hundreds of motions have been litigated, ranging from the treatment of the defendants in US detention and trivial issues surrounding the lawyers. “We had to litigate over the conditions in our office, because it was full of rat faeces and mould,” says Jason Wright, a military lawyer who is also representing Mohammed.

Martins is used to the criticism from the defence and takes it with a large dose of salt. “The defence is a vigilant one; they are clearly under nobody’s thumb,” he says. “But the fairness of the system is not necessarily what the defence counsel says it is.”

…

Hadi al-Iraqi, an alleged senior member of al-Qaeda, listens to charges read out against him, June 18 2014 (Photograph: AP)

There are many ways to measure the endless “war on terror” but detainees have a particular one now. This is their fourth World Cup in custody and the second they have been able to watch. They don’t get it live – it is made available a day later to ensure that nothing subversive seeps through the coverage.

The US military barely acknowledges Camp 7, the facility’s supermax-style facility holding the 9/11 five and other high-value detainees, but the Guantánamo commanders provide the press with a limited tour of Camps 5 and 6, which are modelled on maximum and minimum security prisons in Indiana and Michigan respectively. Talking to detainees is off-limits but guards did allow journalists to look through a one-way glass into one of the indoor living areas flanked by the prisoners’ cells. It was an eerie sight, providing a view with a single fixed camera like a reality TV show, as a few detainees, unaware they were being observed, walked in and out of shot.

One detainee was watching television, laughing and gesturing at the screen as he fiddled with the remote to change channels on a television set encased in a fibre glass box out of physical reach. It was too early in the day for the World Cup and the guards wouldn’t say who the detainees had been supporting.

One camp veteran ventured that they might be getting behind Uruguay. Most countries unconnected to the Middle East don’t want anything to do with taking detainees. Uruguay, however, has agreed to take six.

Richard McGregor is the FT’s Washington bureau chief.