Democrat Ralph Shearer Northam won a hard-fought race to become Virginia's 73rd governor, beating Republican Ed Gillespie in an election watched around the nation as a judgment on President Trump and the politics of polarization.

Voters chose Northam, the lieutenant governor, 54 percent to 45 percent over Gillespie as part of a stunning Democratic sweep of statewide offices, including lieutenant governor and attorney general. There also were widespread Democratic victories in the House of Delegates.

In his victory speech, Northam — a 58-year-old pediatrician and Army veteran — said, "Virginia has told us to end the divisiveness, that we will not condone hatred and bigotry, and to end the politics that have torn this country apart."

The vote had national resonance as well. Democrats — and some moderate Republicans — had rallied behind Northam as a message against the anti-immigrant nativism and angry populism stoked by Trump's surprise victory last year. Gillespie, in turn, had dipped into Trump's playbook with strong law-and-order messages, but tried to keep his distance from the president in a state that now leans blue.

Social media reaction Wednesday framed the Virginia governor's contest as a bellwether of the sentiments across the country, as some people predicted it was also a sign that the GOP faces big trouble ahead. Many voters said they were simply relieved that the election and its ads were over.

One voter, Tina Lee, wrote on Twitter: "Me weeping with relief after checking my phone this AM to find out what happened in my home state."

Democrats at the Northam campaign party in Fairfax City broke into tears as results came in Tuesday evening, the outcome beyond what most had dared hope. For all the fury unleashed on the Virginia races by Trump and his followers, who lit up social media and tried to define the contests in terms of Confederate statues and Hispanic street gangs, Northam had seemed an unlikely standard-bearer to fight back.

Even some fellow Democrats had criticized Northam for his low-key campaign style. But in the end, he won more votes than any previous Virginia governor, and it was a historic night for the party across many fronts.

Voters energized by last fall's demoralizing loss by Hillary Clinton came out in large numbers to elect Democrat Justin Fairfax as lieutenant governor over Republican state Sen. Jill Holtzman Vogel (Fauquier), making Fairfax the first African American elected to a statewide office in Virginia since L. Douglas Wilder won as governor in 1989.

Democratic Attorney General Mark R. Herring was reelected over Republican challenger John Adams.

And Democrats were poised to pick up at least 14 seats in the House of Delegates after fielding a historic number of challengers, many of them women. Among them is Danica Roem, who defeated longtime Republican incumbent Robert G. Marshall in Prince William County to become the first openly transgender person to serve in the Virginia legislature.

Six more House seats were in play as of late Tuesday, with four of those headed for recounts. The Democrats needed to pick up 17 seats to gain control of the House of Delegates. That would be a stunning turnaround in a body where Republicans had a seemingly insurmountable 66-to-34 advantage. All 100 seats were up for election.

"In Virginia, it's going to take a doctor to heal our differences, to bring unity to our people, and I'm here to let you know that the doctor is in," Northam said to ecstatic supporters Tuesday night at George Mason University. "We need to close the wounds that divide, and bring unity to Virginia. . . . Whether you voted for me or not, we are all Virginians. I hope to earn your confidence and support."

Gov. Terry McAuliffe (D), the ultimate party cheerleader and a patron of Northam's political career, said he had not expected such a resounding set of victories — especially in the House of Delegates, where the prospect of regaining a majority had seemed out of reach.

"I always say you're going to get it back because you have to say that politically," McAuliffe said in an interview, "but in my mind I was thinking six to eight [seats gained] would have been a great night for the Democrats."

As he has traveled the country, McAuliffe said, the pressure from other Democrats to perform in this election has been enormous. He recalled that people would say to him through gritted teeth, "We need this."

"This, what a sparkplug," he said. "This is the revitalization of the Democratic Party in America. This isn't just about Virginia tonight."

The victors basked in the idea that they had just shown something to the nation.

"We are so excited tonight to celebrate some incredible victories, not just for the Democratic ticket, not just for the Commonwealth of Virginia, not just the United States but for the world," Fairfax said to his supporters. "The tide is turning for the political climate in this world. . . . We now have a chance to rise to the better angels of our nature, to take our country on a different, more positive course."

Gillespie, 56, was gracious in defeat, taking to the stage at a hotel outside Richmond to congratulate Northam and pledging to help the new governor in any way he could.

"I want to thank all those who voted today, on both sides," Gillespie said, his wife, ticketmates and campaign staffers standing beside him. "These million voters [who supported him] and our friends and family love our commonwealth, they love our fellow Virginians, and they love even those who disagree with them."

Gillespie never mentioned Trump during his concession speech, just as he almost never mentioned him on the campaign trail. But the president was quick to lash out earlier Tuesday as it became clear that Gillespie was losing.

"Ed Gillespie worked hard but did not embrace me or what I stand for," the president tweeted before the final tally was in, and shortly before addressing the South Korean National Assembly during his trip to Asia.

Only hours earlier, he tweeted support for Gillespie, saying that electing "Ralph Northam will allow crime to be rampant in Virginia." But if the Republican wins, Trump said, "MS-13 and crime will be gone." He was referring to the MS-13 street gang, which featured prominently in Gillespie ads raising fears of violence and illegal immigration.

The success of Northam and his ticket was fueled by unprecedented turnout among Democrats and liberals, who traditionally have sat out Virginia elections in nonpresidential years.

Preliminary exit poll results found 28 percent of voters identifying as liberals, up eight points from the 2013 governor's race and two points from last year, when Clinton won the state by five points. Democrats composed 41 percent of the electorate, up four points from 2013 and one point from last year.

Republicans were 31 percent of the electorate, a record low in exit polling dating to 1996.

African Americans accounted for 21 percent of voters, according to exit poll results, identical to their share in last year's presidential election and one point higher than in 2013. In total, nonwhite voters made up 33 percent of the electorate, the same as last year but up from 28 percent in the previous governor's race.

Democrats had worked feverishly in recent weeks to court African American voters, and former president Barack Obama held a rally with the ticket in Richmond last month. Obama also recorded a robo-call that went out Monday and Tuesday to encourage people to vote.

As the national Democratic Party has wrestled with fractures in recent weeks, the Virginia party may have offered a lesson in how to move ahead. After former congressman Tom Perriello mounted a progressive challenge to Northam for the Democratic nomination and lost, he became a foot soldier for Northam in the general election.

Northam also may have benefited from the historic number of Democrats who challenged Republican incumbents in House of Delegates races. Their presence on the ballot helped bring out voters in districts all over the state who otherwise might have had little interest in a nonpresidential election.

Republicans, on the other hand, failed for most of the year to project the same kind of unity. Gillespie ran a restrained primary race and nearly lost to rival Corey Stewart, who fully embraced Trumpian bombast and made defending Confederate statues and fighting illegal immigration into central issues.

After the primary, Stewart refused to endorse Gillespie unless the candidate adopted his hard-right agenda and style. Gillespie gradually leaned in that direction as it became clear that he needed to firm up his base, especially in rural areas, but Stewart never campaigned for him.

Instead, Stewart — who has already said he will challenge Democratic Sen. Tim Kaine next year — made late appearances with Vogel and Adams.

The governor's race had been close in pre-election polling, and Northam had been criticized by some in his party for waging a subdued campaign at a time of high passion and sharp rhetoric. But Virginians turned out in large numbers on a day of patchy rain around the state as Northam and the Democrats relied on an increasingly efficient system for getting voters to the polls, especially in the more-populous parts of the state.

Northam's victory was propelled by white, college-educated women; voters who are concerned about health care; the robust showing among Democrats; and voters who strongly disapprove of Trump, exit polls indicated.

Gillespie ultimately failed in his attempt to walk a very fine line, working for votes in a state where his party's president is deeply unpopular. He resisted even talking about the president for much of the race, while Northam called Trump a "narcissistic maniac" and pledged to be a bulwark against his policies in Virginia.

But Gillespie made a late turn toward Trumpian tactics that seemed to energize his campaign, promising to defend Confederate heritage and airing ads that seemed to equate illegal immigrants with violent gangs.

Trump never campaigned in Virginia for Gillespie, though Vice President Pence appeared with him twice.

The Trump factor drove an unusual amount of national attention toward Virginia, whose election was one of only two statewide contests in the country. The other, in New Jersey, was not considered competitive, so Virginia became the proxy for the painful efforts by both major parties to find their way forward in the age of Trump.

Half of the more than $50 million raised by Virginia's statewide candidates came from outside interest groups, according to an analysis by the nonpartisan Virginia Public Access Project.