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.