Since then, four of the five governors Virginia has elected have been Democrats. Mr. Northam’s victory also handed Democrats a stronger hand to block any Republican attempts at gerrymandering after the next census.
But more than that, his victory was a tonic to an anxious national party that has been reeling since Mr. Trump’s win last year and demoralized by losses in special House elections in Montana and Georgia.
A native of Virginia’s rural Eastern Shore who bears a Tidewater accent that reveals his rural roots, Mr. Northam, 58, was a perhaps an unlikely vessel for the resistance-era Democratic Party. But the left overlooked the two votes he cast for George W. Bush before he entered politics and his otherwise sterling resume — he is a pediatric neurologist and Gulf War veteran — proved far more appealing to the state’s broad middle than Mr. Gillespie’s background as a corporate lobbyist.
The Democrats’ success here was all the more sweet to them because it came as Mr. Gillespie, trailing in the polls, turned to a scorched-earth campaign against Mr. Northam in the race’s final weeks. Mr. Gillespie, a fixture of his party’s establishment who had once warned against the “siren song” of anti-immigrant politics, unleashed a multimillion dollar onslaught linking his rival to a gang with Central American ties and a convicted pedophile who had his rights restored, while also assailing Mr. Northam for wanting to remove Virginia’s Confederate statues.
The strategy appeared to help Mr. Gillespie narrow the gap in the wake of the Charlottesville protests, but it was not enough to overcome the anti-Trump energy in an increasingly diverse state that has not elected a Republican to statewide office since 2009.
Mr. Gillespie, a former Republican National Committee chairman, found it difficult to balance appeals to the president’s unflagging supporters in rural Virginia while simultaneously attempting to win over Mr. Trump’s skeptics in the state’s population centers. He often would not say the president’s name, referring instead to “the administration” or last year’s Republican “ticket.”
Mr. Northam did not have to concern himself with any such political contortions running in a state that has backed the Democratic nominee for president in the last three elections, a striking role reversal from an earlier day here when Virginia Democrats had to distinguish themselves from their more liberal national party.
Indeed, support for Mr. Northam represented a vote for continuity. Gov. Terry McAuliffe, a Democrat barred by state law from seeking re-election, is broadly popular, as are the state’s two Democratic senators, Timothy Kaine and Mark Warner, themselves former governors. Mr. McAuliffe, who was elected in 2013, was the first person in 40 years to win a Virginia governor’s race who was in the same party as the president’s.
Credit Chip Somodevilla/Getty Images
The Virginia election was a rare point of genuine suspense on a map of off-year races dominated by Democrats. In New Jersey, the Democratic ticket established a decisive advantage early in the campaign season, and that lead never flagged. Mr. Murphy, a wealthy Democratic donor who served as ambassador to Germany under former President Barack Obama, ran on a message of rejecting both Mr. Trump and Mr. Christie, who is a politically toxic figure in the state. A cavalcade of major national Democrats marched through the state to back Mr. Murphy, including Mr. Obama, Hillary Clinton and Joseph R. Biden Jr.
National Republicans virtually ignored the race, viewing their nominee, Lt. Gov. Kim Guadagno, as doomed by a deeply hostile political environment and her association with Mr. Christie.
In Virginia, given the state’s steady drift left, Mr. Northam began the race as the front-runner. And he was not expected to have a primary at all.
Mr. Gillespie, also, was assumed to have a lock on his own party’s nomination.
But in an illustration of the turbulent politics that is upending both parties, the two establishment-aligned candidates each faced challenges in the June primaries from candidates making more ideological appeals.
Mr. Gillespie only narrowly averted defeat against Corey Stewart, a Prince William County supervisor and an outspoken Trump backer who oriented his campaign around defending Confederate monuments in Virginia, where there are more such monuments than in any other state. While Mr. Stewart nominally endorsed Mr. Gillespie, he spent much of the general election taunting his former rival, demanding he more fully embrace Mr. Trump and trumpeting his hastily announced 2018 Senate bid against Mr. Kaine.
Mr. Northam easily dispatched his primary opponent, the former congressman Tom Perriello, but was dogged by liberal grumbling throughout the general election that he was insufficiently energetic and unwilling to fully confront Mr. Trump.
After blanketing the state’s airwaves before the primary with an ad in which he savaged the president as “a narcissistic maniac,” Mr. Northam struck a more sober-minded tone during the general election with another widely aired commercial in which he vowed to “work with” Mr. Trump when it is in the interest of Virginia.
With two amiable candidates, the race began as a genteel affair in the high-minded, if not always followed, Virginia tradition.
Neither criticized the other very sharply in a handful of debates and both used their stump speeches to focus chiefly on bolstering the economy of a state that is still heavily dependent on the federal government.
But the tone changed in October when Mr. Gillespie began airing an ad excoriating Mr. Northam for supporting a state measure in support of so-called sanctuary cities, which limit cooperation with federal immigration agents. The commercial featured a group of heavily tattooed men who turned out to be prisoners in a Salvadoran jail.
It was followed not long after by a series of ads focusing on Mr. Northam’s support for the restoration of rights for felons after they are released from prison; these featured a convicted child molester.
Democrats cried foul, but then only seemed to hand Mr. Gillespie a chance to express his own outrage when, in the last week of the campaign, a liberal Hispanic advocacy group aired a commercial portraying a man in a truck with a Gillespie sticker trying to drive into a group of children of color.
The group pulled the spot in the aftermath of the deadly truck attack in New York and Mr. Northam distanced himself from it, but it gave Republicans fodder at the 11th hour.
By Election Day, Mr. Trump could not resist weighing in on the race in his typically bombastic fashion.
“Ralph Northam will allow crime to be rampant in Virginia,” the president wrote on Twitter while traveling in South Korea.