On the Democratic side, Mr. Northam defeated Tom Perriello, a former congressman, who entered the race unexpectedly at the start of the year and mounted an aggressive insurgent campaign that upset the state’s Democratic establishment.
But the threat roused Mr. Northam, 57, a mild-mannered physician. He amplified his rhetoric against Mr. Trump, calling the president “a narcissistic maniac.” And he effectively harnessed the support of every statewide elected Democrat, won a handful of other influential endorsements and spent millions on television ads to repel Mr. Perriello.
The surprisingly close Republican contest foreshadowed Mr. Gillespie’s dilemma heading into the general election: how to handle a president who remains broadly popular on the right but is politically toxic among the broader electorate in Virginia, the only Southern state carried by Hillary Clinton.
Mr. Northam begins the general election with an advantage thanks in large part to Mr. Trump. Nearly 60 percent of Virginia independents disapprove of Mr. Trump, according to a Washington Post poll last month. Virginia is increasingly diverse, especially in its vote-rich urban crescent from the Washington suburbs to Hampton Roads, and it has been drifting away from Republicans, who have not won a statewide election here since 2009.
Mr. Trump has widened the political gulf in a state that was already culturally cleaved among Appalachia, the traditional South and the fast-growing mid-Atlantic. Mr. Gillespie will have to try to create a coalition that melds pro-Trump rural conservatives with anti-Trump suburbanites.
Democrats here are plainly energized about sending a message to Mr. Trump: Turnout spiked from the last time they had a contested primary for governor. After Mr. Perriello, 42, entered the race with slashing speeches against the president, Mr. Northam saw how voters were responding and began speaking out more fiercely. By the end of the campaign, both Democrats were airing commercials attacking Mr. Trump. Now, some Republicans fear that Mr. Northam, an Army veteran from the state’s Eastern Shore, could be difficult to attack as a dogmatic liberal.
His primary victory was a sign that institutional advantages still matter in an increasingly diffuse political era, at least in a statewide election.
He was joined on the final weekend before the election by a trio of popular Virginia Democrats: Gov. Terry McAuliffe and Senators Tim Kaine and Mark Warner. He had the support of every Democrat in the state’s General Assembly, raised more money than Mr. Perriello and outspent him on television in the race’s last weeks.
But some Democratic voters here shrugged off those traditional signs of strength. They preferred Mr. Perriello’s blistering anti-Trump rhetoric, unapologetically liberal politics and stamp of approval from two of the country’s most prominent progressives, Senators Bernie Sanders of Vermont and Elizabeth Warren of Massachusetts.
There was not supposed to be a Democratic race here at all. Mr. McAuliffe, who by state law cannot run for re-election, and every other elected Democrat in the state rallied behind Mr. Northam last year.
But after Mr. Trump’s election, Mr. Perriello, who lost his seat in the House in 2010 and worked for the State Department under President Barack Obama, stunned Virginia’s political establishment by declaring his candidacy. He focused his gaze more on Washington than on Richmond, the state capital, seeming to seize on every controversy facing the Trump administration and eventually calling for the president’s impeachment.
Mr. Perriello’s steady focus on Mr. Trump unsettled some of the more cautious Democrats in Virginia, but it won him attention and money beyond the state’s borders.
Stirred by the threat of losing a nomination he thought was his, Mr. Northam pivoted from stockpiling money from Virginia’s business community to wooing the left. He courted African-American voters, who appeared to strongly support him on Tuesday, and engaged in a back-channel campaign to ensure Mr. Obama’s neutrality in the race.
Mr. Northam called Eric H. Holder Jr., Mr. Obama’s friend and former attorney general, to note that he had supported Mr. Obama’s candidacy in another hotly contested Democratic primary here: his 2008 race for the presidential nomination against Hillary Clinton.
Because of an editing error, an earlier version of this article referred incorrectly in some references to Ed Gillespie. He is not the Republican nominee for governor; a winner has not yet been declared in the race.