Donald Trump dominated the Republican presidential campaign for months. But when it finally counted, when the voters of Iowa finally had their say, they punctured the candidacy of the New York billionaire, delivering a victory to Sen. Ted Cruz of Texas and elevating Sen. Marco Rubio of Florida into the top ranks of the GOP race.

The results scrambled the Republican race as it heads to New Hampshire, giving the GOP a genuine three-way contest that will put Trump to tests he hasn’t been faced with and pitting Cruz and Rubio against one another in a contest both have long been anticipating.

Democrats, too, appeared headed for a prolonged and spirited contest. For much of the night, Hillary Clinton struggled to fend off Sen. Bernie Sanders of Vermont. If she were to prevail, she would be able to claim she had buried the memories of her third-place finish to then-Sen. Barack Obama eight years ago.

But the powerful showing by Sanders, who began as a protest candidate and quickly caught fire with the party’s progressive grass-roots activists, was more than enough for him to claim a moral victory and look to New Hampshire, where he has been leading in the polls, to put Clinton on the defensive.

These maps show how Iowa voters are split between two types of GOP candidates

The Cruz-Rubio matchup is a classic of Republican contests, one seeking to consolidate conservatives and the other favored by the establishment but seeking to appeal across the breadth of the party.

Cruz seeks to energize the party’s grass roots with sharp attacks on Washington elites and a rigidly conservative ideology. Rubio, particularly in the closing days of the Iowa campaign, has preached unity, claiming he above all others has the capacity to bring together a party that has been badly split.

The wild card is Trump, who had shown cross-cutting appeal in polls but who fell short in a state with an electorate long tailor-made for someone with appeal to religious conservatives. He sounded subdued in his brief concession speech Monday night and seemed to take hope from the big lead he has enjoyed in New Hampshire polls.

But New Hampshire voters can be fickle. Trump can’t count on holding onto that support. His message long has been that he is a winner because he is ahead in the polls. Now he heads to New Hampshire a diminished and less-feared candidate.

For Trump, the returns seemed a reminder that huge rallies and a commanding lead in media attention are not enough by themselves to turn out voters. He will head to New Hampshire, where he has had a big lead in the most recent polls, needing to give his candidacy a shot of energy over the next week.

Cruz built his campaign on old-fashioned, methodical organizing coupled with modern and sophisticated modeling and metrics. Although under fire from his rivals in the late stages of the campaign, Cruz was proving to be a strong vote getter, even with high turnout that was supposed to favor Trump.

Lines, lines and more lines: catch a glimpse of what Feb. 1 looked like for caucus-goers in Iowa. (Sarah Parnass/The Washington Post)

Meanwhile, Rubio claimed the prize for exceeding expectations. Aided by a surge of late-deciding voters, the first-term senator ran a very strong third behind Cruz and Trump, vaulting himself into the thick of the early Republican race and separating himself from the other mainstream conservatives competing among one another for supremacy among the establishment candidates.

Cruz won a plurality of evangelical Christians, his most important constituency, and romped among the approximately four in 10 Iowa Republicans who called themselves very conservative. Rubio, however, won a plurality among those who considered themselves somewhat conservative, a constituency that in past Republican nomination battles has been of critical importance.

This was a bad night for the other establishment candidates in the race. Most have invested heavily in New Hampshire, but their collective performance in Iowa was dismal at best. Collectively, former Florida governor Jeb Bush, Ohio Gov. John Kasich and New Jersey Gov. Chris Christie collected just 7 percent of the GOP vote.

Meanwhile, the two previous winners in Iowa, former Arkansas governor Mike Huckabee and former Pennsylvania senator Rick Santorum, won just 3 percent between them.

Exit polls told a tale of two parties, with Republicans exhibiting more dissatisfaction and unrest than the Democrats.

More than 6 in 10 Republicans who attended the caucuses described themselves as evangelical Christians, a slightly higher percentage than four years ago. About four in 10 said they were “angry” with the way the federal government is operating. Almost half said they wanted a nominee from outside the political establishment, compared with four in 10 who said they preferred someone with political experience.

Among Democrats, about two-thirds of caucus attendees described themselves as liberals with about three in 10 saying they are very liberal. Both are about 10 points higher than in 2004 and 2008. About one-third said they prefer the next president to pursue an agenda that is more liberal than President Obama’s, but a 55 percent majority said they want the next president to continue those policies.

A campaign that began in a predictable way a year ago, with Clinton seemingly facing only nominal opposition and a stable of Republican governors jockeying for dominance, was by the summer a campaign turned upside down, thanks to the candidacies of Trump and Sanders.

The atmosphere was shaped by the long shadows of the economic collapse of 2008, political dysfunction and gridlock in Washington, and by the all-too-recent terrorist attacks in Paris and San Bernardino, Calif. Together they created a backdrop of anger and anxiety, stoking fears about the future.

The prelude to Monday’s caucuses was anything but conventional. Instead it was a months-long first act that was as tumultuous as it was confounding, all of which caught many political strategists — and some candidates — by surprise.

Trump’s candidacy gave voice to the anger and disaffection within the country that has been building for several years. His harsh attacks on illegal immigrants and his promise to build a wall on the U.S.-Mexico border were about more than immigration. They were emblematic of the feeling among many Americans that the country is sliding away from traditional virtues and values.

Having lost the first contest of the year, the question is what kind of candidate Trump will become. He attacked Cruz relentlessly in Iowa, questioning whether he was eligible to be president because Cruz was born in Canada to an American mother. But Cruz’s supporters proved loyal in the face of those attacks.

In the Democratic race, Sanders’s candidacy took root in the frustrations on the left. His populist attacks on big banks and Wall Street executives who had escaped prosecution after the 2008 financial collapse instantly resonated with the party’s liberal wing.

Sanders also struck a chord with his attacks on the role of money in politics, railing against the power of super PACs and the influence of billionaires on campaigns and the legislative process.

Sanders’s strength was the power of his message, a sharp attack against the elites and a big-government agenda promising universal health care, free college at state schools and other costly programs. His message proved to be a powerful motivating force, but he will face stepped-up criticism from Clinton about who can ultimately advance a progressive agenda.

Clinton put her faith in Iowa in her organization, one built on the lessons and mistakes of her 2008 campaign. When she appeared before supporters late Monday, she declined to claim outright victory — only saying that she was breathing a “sigh of relief” at the numbers. As Monday began, she was hoping for far more than that and will have to brace for the kind of contest she never envisioned when she launched her candidacy.