“This is a historic election, and I recognize the significance it has for African-Americans and for the special pride that must be theirs tonight,” Mr. McCain said, adding, “We both realize that we have come a long way from the injustices that once stained our nation’s reputation.”

Not only did Mr. Obama capture the presidency, but he led his party to sharp gains in Congress. This puts Democrats in control of the House, the Senate and the White House for the first time since 1995, when Bill Clinton was in office.

The day shimmered with history as voters began lining up before dawn, hours before polls opened, to take part in the culmination of a campaign that over the course of two years commanded an extraordinary amount of attention from the American public.

As the returns became known, and Mr. Obama passed milestone after milestone —Ohio, Florida, Virginia, Pennsylvania, New Hampshire, Iowa and New Mexico — people rolled spontaneously into the streets to celebrate what many described, with perhaps overstated if understandable exhilaration, a new era in a country where just 143 years ago, Mr. Obama, as a black man, could have been owned as a slave.

For Republicans, especially the conservatives who have dominated the party for nearly three decades, the night represented a bitter setback and left them contemplating where they now stand in American politics.

Mr. Obama and his expanded Democratic majority on Capitol Hill now face the task of governing the country through a difficult period: the likelihood of a deep and prolonged recession, and two wars. He took note of those circumstances in a speech that was notable for its sobriety and its absence of the triumphalism that he might understandably have displayed on a night when he won an Electoral College landslide.

“The road ahead will be long, our climb will be steep,” said Mr. Obama, his audience hushed and attentive, with some, including the Rev. Jesse Jackson, wiping tears from their eyes. “We may not get there in one year or even one term, but America, I have never been more hopeful than I am tonight that we will get there. I promise you, we as a people will get there.” The roster of defeated Republicans included some notable party moderates, like Senator John E. Sununu of New Hampshire and Representative Christopher Shays of Connecticut, and signaled that the Republican conference convening early next year in Washington will be not only smaller but more conservative.