His videotaped death was a very public end to a hidden ordeal. The story of what happened in the Islamic State’s underground network of prisons in Syria is one of excruciating suffering. Mr. Foley and his fellow hostages were routinely beaten and subjected to waterboarding. For months, they were starved and threatened with execution by one group of fighters, only to be handed off to another group that brought them sweets and contemplated freeing them. The prisoners banded together, playing games to pass the endless hours, but as conditions grew more desperate, they turned on one another. Some, including Mr. Foley, sought comfort in the faith of their captors, embracing Islam and taking Muslim names. Their captivity coincided with the rise of the group that came to be known as the Islamic State out of the chaos of the Syrian civil war. It did not exist on the day Mr. Foley was abducted, but it slowly grew to become the most powerful and feared rebel movement in the region. By the second year of Mr. Foley’s imprisonment, the group had amassed close to two dozen hostages and devised a strategy to trade them for cash. It was at that point that the hostages’ journeys, which had been largely similar up to then, diverged based on actions taken thousands of miles away: in Washington and Paris, in Madrid, Rome and beyond. Mr. Foley was one of at least 23 Western hostages from 12 countries, a majority of them citizens of European nations whose governments have a history of paying ransoms. Their struggle for survival, which is being told now for the first time, was pieced together through interviews with five former hostages, locals who witnessed their treatment, relatives and colleagues of the captives, and a tight circle of advisers who made trips to the region to try to win their release. Crucial details were confirmed by a former member of the Islamic State, also known as ISIS, who was initially stationed in the prison where Mr. Foley was held, and who provided previously unknown details of his captivity. The ordeal has remained largely secret because the militants warned the hostages’ families not to go to the news media, threatening to kill their loved ones if they did. The New York Times is naming only those already identified publicly by the Islamic State, which began naming them in August. Officials in the United States say they did everything in their power to save Mr. Foley and the others, including carrying out a failed rescue operation. They argue that the United States’ policy of not paying ransoms saves Americans’ lives in the long run by making them less attractive targets. Inside their concrete box, the hostages did not know what their families or governments were doing on their behalf. They slowly pieced it together using the only information they had: their interactions with their guards and with one another. Mostly they suffered, waiting for any sign that they might escape with their lives. The Grab It was only a 40-minute drive to the Turkish border, but Mr. Foley decided to make one last stop. In Binesh, Syria, two years ago, Mr. Foley and his traveling companion, the British photojournalist John Cantlie, pulled into an Internet cafe to file their work. The two were no strangers to the perils of reporting in Syria. Only a few months earlier, Mr. Cantlie had been kidnapped a few dozen miles from Binesh. He had tried to escape, barefoot and handcuffed, running for his life as bullets kicked up the dirt, only to be caught again. He was released a week later after moderate rebels intervened. They were uploading their images when a man walked in. “He had a big beard,” said Mustafa Ali, their Syrian translator, who was with them and recounted their final hours together. “He didn’t smile or say anything. And he looked at us with evil eyes.” The man “went to the computer and sat for one minute only, and then left directly,” Mr. Ali said. “He wasn’t Syrian. He looked like he was from the Gulf.” Mr. Foley, an American freelance journalist filing for GlobalPost and Agence France-Presse, and Mr. Cantlie, a photographer for British newspapers, continued transmitting their footage, according to Mr. Ali, whose account was confirmed by emails the journalists sent from the cafe to a colleague waiting for them in Turkey. Photo James Foley edited video from Aleppo, Syria, two weeks before he was kidnapped in November 2012.CreditNicole Tung More than an hour later, they flagged a taxi for the 25-mile drive to Turkey. They never reached the border. The gunmen who sped up behind their taxi did not call themselves the Islamic State because the group did not yet exist on Nov. 22, 2012, the day the two men were grabbed. But the danger of Islamic extremism was already palpable in Syria’s rebel-held territories, and some news organizations were starting to pull back. Among the red flags was the growing number of foreign fighters flooding into Syria, dreaming of establishing a “caliphate.” These jihadists, many of them veterans of Al Qaeda’s branch in Iraq, looked and behaved differently from the moderate rebels. They wore their beards long. And they spoke with foreign accents, coming from the Persian Gulf, North Africa, Europe and beyond.A van sped up on the left side of the taxi and cut it off. Masked fighters jumped out. They screamed in foreign-accented Arabic, telling the journalists to lie on the pavement. They handcuffed them and threw them into the van. They left Mr. Ali on the side of the road. “If you follow us, we’ll kill you,” they told him. Over the next 14 months, at least 23 foreigners, most of them freelance journalists and aid workers, would fall into a similar trap. The attackers identified the locals whom journalists hired to help them, like Mr. Ali and Yosef Abobaker, a Syrian translator. It was Mr. Abobaker who drove Steven J. Sotloff, an American freelance journalist, into Syria on Aug. 4, 2013. “We were driving for only 20 minutes when I saw three cars stopped on the road ahead,” he said. “They must have had a spy on the border that saw my car and told them I was coming.” The kidnappings, which were carried out by different groups of fighters jousting for influence and territory in Syria, became more frequent. In June 2013, four French journalists were abducted. In September, the militants grabbed three Spanish journalists. Checkpoints became human nets, and last October, insurgents waited at one for Peter Kassig, 25, an emergency medical technician from Indianapolis who was delivering medical supplies. In December, Alan Henning, a British taxi driver, disappeared at another. Mr. Henning had cashed in his savings to buy a used ambulance, hoping to join an aid caravan to Syria. He was kidnapped 30 minutes after crossing into the country. The last to vanish were five aid workers from Doctors Without Borders, who were plucked in January from the field hospital in rural Syria where they had been working. The Interrogation At gunpoint, Mr. Sotloff and Mr. Abobaker were driven to a textile factory in a village outside Aleppo, Syria, where they were placed in separate cells. Mr. Abobaker, who was freed two weeks later, heard their captors take Mr. Sotloff into an adjoining room. Then he heard the Arabic-speaking interrogator say in English: “Password.” It was a process to be repeated with several other hostages. The kidnappers seized their laptops, cellphones and cameras and demanded the passwords to their accounts. They scanned their Facebook timelines, their Skype chats, their image archives and their emails, looking for evidence of collusion with Western spy agencies and militaries. “They took me to a building that was specifically for the interrogation,” said Marcin Suder, a 37-year-old Polish photojournalist kidnapped in July 2013 in Saraqib, Syria, where the jihadists were known to be operating. He was passed among several groups before managing to escape four months later.