Democrats completed Virginia’s historic partisan shift from red to blue on Tuesday, winning majorities in both chambers of the legislature and consolidating power across state government for the first time in a generation.
In an election where passions about President Trump and the impeachment inquiry drove voters on both sides, a revolt against the president in Virginia’s rapidly growing suburbs helped remake the state’s political map. Now, under Gov. Ralph Northam, who survived scandal earlier this year, Democrats are positioned to advance a set of sweeping liberal priorities.
Going into Tuesday, Republicans held a 20 to 19 advantage in the State Senate and a 51 to 48 edge in the House of Delegates, with one vacancy in each chamber. Democrats picked up at least two Senate seats, including an upset in a suburban Richmond district by Ghazala Hashmi, who will be the first Muslim woman in the Senate.
A former college literature professor, Ms. Hashmi was brought to the country from India as a child. Running her first campaign, she described experiencing a personal crisis after Mr. Trump ordered a ban on immigration from Muslim-majority countries.
“I didn’t know if I actually had a home in this country,” she said in an interview before the voting. “My anxiety was caused by wondering if other people would speak up and support the assault we were seeing on civil liberties.” She decided to speak up and represent herself.
Other notable winners on Tuesday included Shelly Simonds, a Democrat who lost a House race in 2017 in a random drawing after the votes produced a dead tie. In a rematch, Ms. Simonds defeated the Republican incumbent, David Yancey.
Chris Jones, the powerful Republican head of the House appropriations committee, was upset by Clint Jenkins, a small-business owner. And Tim Hugo, the last Republican in the suburbs of Northern Virginia, lost to Dan Helmer in a district that has become more than a quarter Asian and Hispanic — emblematic of the demographic shifts in the state that have remade Virginia, once the seat of the Confederacy.
After gaining 15 seats in the House in 2017, Democrats this year picked up at least another five, a signal that the state’s blue groundswell had lost little momentum in the third year of the Trump presidency. The ultimate size of the new Democratic majorities was in flux late Tuesday as a few close races were undecided.
“When the House convenes in January, we will welcome new members on both sides of the aisle, and, for the first time in two decades, a new party will sit in the majority,’’ Kirk Cox, the outgoing Republican speaker of the House, who won re-election, said in a statement.
In the days ahead of Election Day, Republicans warned direly that conservatives faced an existential crisis, in which they could lose the Virginia they had known, while Democrats argued their opponents had too long tried to block progress on issues like Medicaid expansion, a higher minimum wage and gun safety.
Pre-election polling showed the top issues were all ones that favored Democrats: raising the minimum wage to $15, spending more on roads and, especially, after a mass shooting in Virginia Beach this year, expanding background checks and banning assault weapons.
After Mr. Northam called a special session in of the legislature in July to respond to gun violence, and the Republican majorities adjourned it after just 90 minutes, Democrats thought they had won a moral victory to use against opponents in the fall. Republicans denounced the session as a political stunt.
“The era of Republican obstruction in the Commonwealth of Virginia is now over,’’ former Gov. Terry McAuliffe said in a statement. “While tonight we celebrate the history we have made, tomorrow we must begin rewarding voters with action.”
Just months ago, Democrats seemed badly wounded heading into an election year as scandal engulfed Mr. Northam and the other top two Democrats in the state. After a racist photo on Mr. Northam’s medical school yearbook page surfaced in February, he initially acknowledged that he was in the photo. He quickly reversed himself. The party was soon thrown further into turmoil, as two women accused Lt. Gov. Justin Fairfax of sexual assault, which he denied.
But the headlines faded, offering Republicans less ammunition than they once expected. Recent polls show more voters approve of the governor’s performance than disapprove.
The balance of power in both chambers turned on just a handful of competitive districts, all in suburban regions outside Washington, Richmond and in Hampton Roads.
Swing-district Republicans backpedaled away from Mr. Trump and the party, a brand that had proved toxic in last year’s midterms with the increasingly diverse electorate in suburbia. Some Republicans campaigned more like Democrats, boasting of support for Medicaid expansion that the party long fought in Richmond, for L.G.B.T. rights and even for gun safety measures.
With so much at stake — a referendum on the president, the partisan balance of both houses, political momentum going into a presidential election — money cascaded into the normally low-interest legislative races.
More than $1 million flowed to 16 individual candidates, who were seeking part-time jobs that pay less than $20,000 a year. Races where television ads had been unheard-of echoed with a cacophony of attacks while Virginians tried to tune out politics for a brief respite during the World Series.
Emily’s List, which supports women running for office who back abortion rights, pumped more than $2 million into Democratic races mostly for the House. The former New York mayor Michael R. Bloomberg was the largest outside individual donor. His group Everytown for Gun Safety gave $2.5 million to help Democrats, and Beyond Carbon, his climate change group, donated more than $600,000 to two House candidates in coastal districts.
Republicans’ largest donor was the Republican State Leadership Committee ($3.2 million), a national group that supports state-level races, the value of which became clear after sweeping Republican legislative victories in 2010, which led to Republican-drawn voting maps that have influenced power in the states and in Congress.
State senators, who serve four-year terms, had not faced voters since 2015, before Mr. Trump’s election. Many Republican senators who were considered most vulnerable occupied districts carried by Mr. Northam in 2017 and by three Democrats who flipped congressional races in 2018.
Another factor aiding Democrats was a court-ordered remapping of districts in southeast Virginia. In June, the United States Supreme Court upheld a lower court decision striking down earlier maps as racially gerrymandered. New maps shifted 425,000 voters in 25 districts. They more evenly distributed black voters, which gave Democrats an overall advantage, according to an analysis by the nonpartisan Virginia Public Access Project.
This year, the prospect of the 2021 redistricting after the next census was a quiet but powerful issue for both parties.
In recent days, nationally prominent Democrats campaigned alongside statehouse candidates to raise voters’ enthusiasm, including the presidential candidates Joseph R. Biden Jr., Amy Klobuchar and Kamala Harris.
On Saturday, Senator Mark Warner, Democrat of Virginia, sought to inspire volunteer door-knockers by citing what he viewed as Republican outrages in Washington, including Mr. Trump’s abandonment of Kurdish allies in Syria and smears against a decorated Army officer called to testify in the impeachment inquiry.
He suggested Democrats could force congressional Republicans to “grow a backbone” by flipping the Virginia House and Senate.
Republican surrogates were less visible. Mr. Trump skipped campaigning for fellow Republicans in the state, though Vice President Mike Pence held a rally in Virginia Beach.
At a get-out-the-vote gathering on Saturday for the Henrico County Republican Committee, once a Republican stronghold outside Richmond, the headliner was a local talk radio host, John Reid.
“I think Democrats have overreached beyond belief,” Mr. Reid told the party faithful, referring to impeachment proceedings. He predicted Republicans would have a good day on Tuesday. “Donald Trump upsets people — that’s fine,” he said. “It cannot be denied that he’s delivered.”