Hillary Clinton spoke to supporters, Nov. 9, offering a message of thanks, apology and hope. Here are the key moments from that fervent address. (Sarah Parnass/The Washington Post)

Hillary Clinton saw a rancorous campaign through to its bitter end on Wednesday, conceding the presidency to a man she had called unfit for the office and a threat to the fabric of the country.

In defeat, Clinton turned one of her central attacks on Donald Trump into a charge for the presidency she was denied, urging the next president and her disheartened supporters to respect the peaceful transfer of power.

“We have seen that our nation is more deeply divided than we thought,” Clinton said hours after losing a presidential election she had been widely seen as sure to win. “But I still believe in America, and I always will. And if you do, then we must accept this result and then look to the future.”

Speaking to a hastily assembled group of campaign staff, volunteers and supporters, Clinton went on: “Donald Trump is going to be our president. We owe him an open mind and the chance to lead.”

She grew misty-eyed but maintained her composure — even as her supporters could not — throughout a speech she never imagined giving. She spoke in a hotel ballroom farther in feel than in distance from the exquisitely planned celebration she did not have on Tuesday night.

Anti-Trump protests took place near some college campuses on election night. (Monica Akhtar/The Washington Post)

Hours earlier, Clinton had also planned to speak of the promise of “an America that’s hopeful, inclusive and big-hearted.” She had planned to call for unity and healing, with her own history-making election as the first female president a galvanizing example.

That dream started to unravel about 8:30 p.m. Tuesday, when roughly half the states had voted and results showed a Trump lead that Clinton’s confident, data-driven campaign had not foreseen.

Clinton’s second unsuccessful run for the White House relied heavily on a complex computer algorithm that the campaign was prepared to publicly unveil after the election.

As election results rolled in Tuesday, Clinton watched from a suite in the Peninsula Hotel in Manhattan as her would-be electoral advantage disappeared. The gulf opened and never closed as Trump performed more strongly than expected in several upper-Midwestern states that had voted for Democrats for a generation.

Earlier in the night, top Clinton aides bounced around the room doing media interviews and chatting with reporters, projecting optimism that Clinton’s voters would come through with record turnout.

But by 9 p.m., the early warning signs had become dire. Virginia, a state that they thought would be an easy win, was looking like a squeaker. Florida had begun to appear out of reach. And Trump’s victories in Iowa and Ohio, which were expected, became the leading edge of a Trump wave that swamped Clinton.

Not only did Trump capture big margins in battleground states, outperforming expectations for a divisive and damaged candidate, but he outperformed 2012 nominee Mitt Romney in some categories. Meanwhile, Clinton underperformed with the groups she needed most — minorities and younger voters — showing that she could neither recreate the much-admired “Obama coalition” nor assemble what her aides had begun to call her own “Hillary coalition.”

The 2016 presidential election, in graphics

One by one, Clinton aides began disappearing from the main hall and not answering their phones. They retreated to a nearby war room, which was cordoned off and guarded, and never returned. The mood outside the room turned from confusion to disbelief, anger and dejection.

Back at the Peninsula, campaign aides who were not part of Clinton’s small core of intimate friends and advisers gradually peeled away and came to the Javits Center in ones and twos. Eventually it was only Clinton, her family and the closest advisers, including Huma Abedin and Cheryl Mills, making the decision first to put off a concession speech and then to concede by phone.

Mills smiled softly and shook her head when asked Wednesday what those hours were like.

“It’s too raw,” she said as she talked with Clinton supporters outside the New Yorker Hotel, where Clinton, flanked by Sen. Tim Kaine (Va.), her running mate, and her husband and daughter, had said goodbye.

Clinton did not dwell Wednesday on what other friends said was an agonizing reckoning in that hotel room, but the mood was akin to the funeral for a sudden death — shocking, mystifying and starkly real. It was clear that no one — not Clinton, not her husband, not her supporters, not the core team of her campaign — had seen it coming.

“I know how disappointed you feel because I feel it, too, and so do tens of millions of Americans who invested their hopes and dreams in this effort,” Clinton said. “This is painful, and it will be for a long time.”

Time and time again, Clinton and the team seemed to miss the magnitude of the forces that would overtake her in the Democratic primary and later against Trump.

It wasn’t until Sen. Bernie Sanders of Vermont showed surprising strength in Iowa that the Clinton campaign put real stock in the strength of his populist economic message. The same forces helped Trump’s win on Tuesday.

Clinton would need voters to choose her over her opponents because of how she would “handle the economy and relate to the middle class,” her campaign manager Robby Mook wrote in an email to close advisers in March 2014.

But more often, the effort to disqualify Trump based on his temperament took precedence over an economic focus.

Clinton’s aides knew that there were soft spots in the Obama voting coalition that could pose problems for Clinton’s bid. Her support among young voters was fragile, and in early voting, African American turnout and enthusiasm lagged.

In the end, Clinton’s inability to bring out Democratic voters in the election was a dramatic failure that left her more than 5 million votes shy of Obama’s total in 2012, according to preliminary results. Trump was ultimately able to claim victory having earned fewer votes than Romney did in 2012.

Latino voters did turn out in 2016, but many more than expected were willing to give Trump a shot at the White House. Among nonwhite voters, Clinton led Trump by 54 points — a whopping advantage but less than Obama’s 61-point lead four years ago.

These miscalculations probably cost Clinton key states that Obama won four years ago: Michigan, Wisconsin, Florida and Pennsylvania. Clinton walked away having won the popular vote but having lost badly to Trump in the electoral college.

While her reliance on analytics became well known, the particulars were kept hush-hush, including the existence of a powerful computer algorithm nicknamed “Ada” after a 19th-century female mathematician. According to aides, the algorithm operated on a separate server than the rest of the Clinton operation as a security precaution, and only a few senior aides had access.

According to aides, a raft of polling numbers, public and private, were fed into the algorithm, as well as ground-level voter data collected by the campaign. Once early voting began, those numbers were factored in, too.

With that, aides said, Ada ran 400,000 simulations a day of what the race against Trump might look like. It spat out a report giving campaign manager Robby Mook and others a detailed picture of which battleground states were most likely to tip the race in one direction or another — and guiding decisions about where to spend time and deploy resources.

But was it the right guidance? It appears that the importance of some states Clinton would lose — including Michigan and Wisconsin — never became fully apparent or that it was too late once it did.

Clinton made several visits to Michigan during the general election, but it wasn’t until the final days that she, Obama and her husband made a concerted effort.

As for Wisconsin, Clinton didn’t make any general-election appearances there at all.

Said Mook, in a wee-hours thank-you note to campaign workers: “Campaigns are incredibly hard, and sometimes the results don’t reflect the merit, work and commitment that goes into them. This is one of those times.”

Early afternoon on Wednesday, campaign staff trickled into the Brooklyn headquarters for food, ice cream and a staff meeting following Clinton’s concession speech.

They huddled over cellphones, listening to Clinton, who spoke to them and hundreds of others scattered across the country, thanking them for their “work and tireless dedication,” according to a campaign aide who spoke on the condition of anonymity to discuss the details of the meeting.

Later, Mook, who has commanded nearly universal loyalty among staffers, led the pep talk, acknowledging the disappointing results.

“You were fighting on the right side,” Mook told them, according to the aide. “Hillary was our champion.”

Podesta added praise for Mook, whose data-driven method of managing the campaign will probably come under intense scrutiny after the campaign failed to anticipate Tuesday’s upset.

“We have the number one campaign manager,” Podesta told them. “I’ve been doing this since 1968, and I’ve never seen a culture and a spirit like we created in this campaign.”

John Wagner in Washington contributed to this report.