Mr. McAuliffe, 56, ran as a social liberal and an economic moderate focused on job creation. Mr. Cuccinelli, a Republican who was the first attorney general to sue over President Obama’s health care law, ran as a hard-line social conservative and aimed his campaign almost exclusively at the Tea Party wing of his party.
Still, despite substantially outraising Mr. Cuccinelli, $34.4 million to $19.7 million, Mr. McAuliffe won by a margin — just over two percentage points — that was smaller than some pre-election polls had suggested.
Mr. McAuliffe benefited from an electorate that was less white and less Republican than it was four years ago. He drew about as large a percentage of African-Americans as Mr. Obama did last year. Blacks accounted for one in five voters, according to exit polls conducted by Edison Research. Mr. Cuccinelli’s strong anti-abortion views also brought out opponents, with 20 percent of voters naming abortion as their top issue; Mr. McAuliffe overwhelmingly won their support. The top issue for voters was the economy, cited by 45 percent in exit polls.
In a victory speech here, Mr. McAuliffe thanked the “historic number of Republicans who crossed party lines to support me” and invoked a tradition of bipartisanship in Richmond, the capital. In a checklist of recent governors who had moved the economy forward, he included the incumbent, Bob McDonnell, a Republican.
“Over the next four years, most Democrats and Republicans in Virginia want to make Virginia a model for pragmatic leadership that is friendly to job creation,” Mr. McAuliffe said.
His tone was notably more conciliatory than that of Mr. Cuccinelli, who struck a defiant note at a rally in Richmond, interpreting the closeness of the race to a rejection of Mr. Obama’s health care law. “Despite being outspent by an unprecedented $15 million, this race came down to the wire because of Obamacare,” Mr. Cuccinelli said, adding, “We were lied to by our own government.”
That Mr. McAuliffe was elected in a onetime Republican stronghold while unapologetically supporting gun restrictions, same-sex marriage and abortion rights will no doubt be scrutinized by both parties, particularly by Republicans concerned about the appeal of the Tea Party in swing states and districts ahead of the 2014 midterm elections. And Mr. Cuccinelli’s defeat in a Southern state will no doubt be contrasted with the Republicans’ great success of the day, the dominating re-election of Gov. Chris Christie of New Jersey, who appeals to swaths of Democrats. But the close result, after a race in which Mr. Cuccinelli was substantially outspent, could make it difficult to draw firm conclusions.
Mr. Cuccinelli, 45, whose passionate base seemed to give him an early edge in a race between two flawed candidates, rattled business-oriented Republicans. A surprising roster of the party’s establishment — including Will Sessoms, the mayor of the largest city, Virginia Beach — endorsed Mr. McAuliffe.
Mr. McAuliffe’s career as a wealthy business investor yielded many unflattering and, critics said, possibly unethical details. But he neutralized the issue by arguing that Mr. Cuccinelli’s social agenda, which included hostile comments about homosexuality, staunch opposition to abortion and an attempt to discredit a climate scientist at the University of Virginia, would give the state a retrograde image that would deter businesses from moving here.
A key issue was Mr. McAuliffe’s embrace of a roads bill championed and signed by Mr. McDonnell, which Mr. Cuccinelli opposed because it raised taxes. In rapidly growing Northern Virginia, snarled traffic is the chief concern of chambers of commerce and Mr. McAuliffe was able to portray himself as pro-business and bipartisan.
Although a majority of female voters chose Mr. McDonnell four years ago, Mr. Cuccinelli trailed Mr. McAuliffe among women by nearly 10 percentage points. Nearly seven in 10 unmarried women supported Mr. McAuliffe.
Both Planned Parenthood and the Susan B. Anthony List, an anti-abortion group, poured money into the race. Abortion rights groups created graphic television ads linking the Republican ticket to a failed state bill in 2012 that would have required vaginal ultrasounds for women seeking abortions.
The McAuliffe campaign pounded on Mr. Cuccinelli’s support for failed “personhood” bills that could have banned some common forms of birth control, and for being one of only three attorneys general in the country to oppose the federal Violence Against Women Act.
Six months ago, the race seemed Mr. Cuccinelli’s to lose. He was a conservative of impeccable credentials and a national figure because of the lawsuit over the president’s health care law in 2010. Mr. McAuliffe had drawn an unserious self-portrait in his 2007 memoir, “What a Party!,” including a story about leaving his wife, Dorothy, in the car with their newborn child to duck into a Democratic fund-raiser.
Mr. McAuliffe’s previous bid for governor, in 2009, ended in a humiliating defeat in the primary after he was accused of being a carpetbagger. His effort to strengthen his business ties to Virginia through an electric car company, GreenTech, backfired when he set up production in Mississippi and news reports revealed the company was the target of federal investigators.
But Mr. Cuccinelli was unable to profit from the tarnishing of Mr. McAuliffe because the attorney general had his own problem with a political gifts scandal emanating from the governor. A benefactor of Mr. McDonnell’s who lavished him and his wife with a Rolex watch and other favors also gave Mr. Cuccinelli and his family vacations at a lake home.
Mr. Cuccinelli secured the Republican nomination in May by packing the state party with his supporters, who chose to skip a primary in favor of a nominating convention, ensuring a more ideological slate of candidates.
In the middle of the federal government shutdown, which hit hard in Virginia, with its many federal workers and its defense industry, Mr. Cuccinelli appeared at a family values rally with Senator Ted Cruz of Texas, the architect of the shutdown. In exit polls, about a third of voters said the shutdown had affected someone in their household, and most of them voted for Mr. McAuliffe.
Mr. Cuccinelli spent the final weeks of the campaign barnstorming with national Tea Party stars. He appeared on election eve with the former presidential candidate Ron Paul, meant to call home votes from a third-party candidate, Robert Sarvis, a libertarian.
Meanwhile, with money pouring into Mr. McAuliffe’s campaign, thanks to his ties to major donors, including supporters of the Clintons, he set off an avalanche of negative ads. Mr. McAuliffe outspent his opponent by nearly 75 percent, and beginning in late summer drove up Mr. Cuccinelli’s unfavorable ratings, where they remained.
Mr. McAuliffe ran a disciplined campaign, touring all 23 community colleges in the state to highlight work force development and keeping his message tightly on job creation.
In the last week, Mr. Cuccinelli seized the chance to pivot to the disastrous debut of the federal health insurance marketplace. The issue may have narrowed Mr. McAuliffe’s victory margin, but in the end it was not enough.
Mr. McAuliffe broke a 36-year pattern in which Virginia’s governor, picked the year after the presidential election, came from the party out of power in the White House. The political scientist who first remarked on the trend, Larry J. Sabato of the University of Virginia, ascribed it to a natural tendency toward buyer’s remorse. But this year, as unpopular as Mr. Obama and his health care law may be with many Virginians, “dislike of Cuccinelli is even stronger,” Mr. Sabato said.

Dalia Sussman contributed reporting from New York.