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.