LONDON — Britain has voted to leave the European Union, a historic decision sure to reshape the nation’s place in the world, rattle the Continent and rock political establishments throughout the West.
Not long after the vote tally was completed, Prime Minister David Cameron, who led the campaign to remain in the bloc, appeared in front of No. 10 Downing Street to announce that he planned to step down by October, saying the country deserved a leader committed to carrying out the will of the people.
The stunning turn of events was accompanied by a plunge in the financial markets, with the value of the British pound and stock prices in Asia plummeting.
The margin of victory startled even proponents of a British exit. The “Leave” campaign won by 52 percent to 48 percent. More than 17.4 million people voted in the referendum on Thursday to sever ties with the European Union, and about 16.1 million to remain in the bloc.
“I will do everything I can as prime minister to steady the ship,” Mr. Cameron said, adding, “I do not believe it right for me to try to be the captain that steers the ship” in the long term.
Despite opinion polls before the referendum that showed either side in a position to win, the outcome nonetheless stunned much of Britain, Europe and the trans-Atlantic alliance, highlighting the power of anti-elite, populist and nationalist sentiment at a time of economic and cultural dislocation.
“Dare to dream that the dawn is breaking on an independent United Kingdom,” Nigel Farage, the leader of the U.K. Independence Party, one of the primary forces behind the push for a referendum on leaving the European Union, told cheering supporters just after 4 a.m.
Chuka Umunna, a Labour lawmaker, called the vote “a seismic moment for our country.” Keith Vaz, another Labour legislator, said: “This is a crushing decision, this is a terrible day for Britain and a terrible day for Europe. In 1,000 years, I would never have believed that the British people would vote for this.”
In Berlin, the German foreign minister, Frank-Walter Steinmeier, called the news “truly sobering” and said, “It looks like a sad day for Europe and for Britain.”
Britain will become the first country to leave the 28-member bloc, which has been increasingly weighed down by its failures to deal fully with a succession of crises, from the financial collapse of 2008 to a resurgent Russia and the huge influx of migrants last year.
It was a remarkable victory for the country’s anti-Europe forces, which not long ago were considered to have little chance of prevailing.
Financial markets, which had been anticipating that Britain would vote to stay in, started plunging before the vote tally was complete, putting pressure on central banks and regulators to take steps to guard against a spread of the damage. Economists had predicted that a vote to leave the bloc could do substantial damage to the British economy.
Mr. Cameron had vowed before the vote to move quickly to begin the divorce process if Britain voted to leave. But he suggested on Friday that he would seek to calm the atmosphere before taking any action. In the meantime, nothing will change immediately on either side of the Channel, with existing trade and immigration rules remaining in place. The withdrawal process is expected to be complex and contentious, though under the bloc’s governing treaty it is effectively limited to two years.
For the European Union, the result is a disaster, raising questions about the direction, cohesion and future of a bloc built on liberal values and shared sovereignty that represents, with NATO, a vital component of Europe’s postwar structure.
Britain is the second-largest economy after Germany in the European Union, a nuclear power with a seat on the United Nations Security Council, an advocate of free-market economics and a close ally of the United States.
The loss of Britain is an enomous blow to the credibility of a bloc already under pressure from slow growth, high unemployment, the migrant crisis, Greece’s debt woes and the conflict in Ukraine.
“The main impact will be massive disorder in the E.U. system for the next two years,” said Thierry de Montbrial, founder and executive chairman of the French Institute of International Relations. “There will be huge political transition costs, on how to solve the British exit, and the risk of a domino effect or bank run from other countries that think of leaving.”
While leaders of the Leave campaign spoke earnestly about sovereignty and the supremacy of Parliament or in honeyed tones about “the bright sunlit uplands” of Britain’s future free of Brussels, it was anxiety about immigration — membership in the European Union means freedom of movement and labor throughout the bloc — that defined and probably swung the campaign.
With net migration to Britain of 330,000 people in 2015, more than half of them from the European Union, Mr. Cameron had no effective response to how he could limit the influx. And there was no question that while the immigrants contributed more to the economy and to tax receipts than they cost, parts of Britain felt that its national identity was under assault and that the influx was putting substantial pressure on schools, health care and housing.
The campaign run by one of the loudest proponents of leaving, the U.K. Independence Party, flirted with xenophobia, nativism and what some of its critics considered racism. But the official, more mainstream Leave campaign also invoked immigration as an issue, and its slogan, “Take control,” resonated with voters who feel that the government is failing to regulate the inflow of people from Europe and beyond.
Other anti-establishment and far-right parties in Europe, like the National Front of Marine Le Pen in France, Geert Wilders’s party in the Netherlands and the Alternative for Germany party will celebrate the outcome. The depth of anti-Europe sentiment could be a key factor in national elections scheduled next year in the other two most important countries of the European Union, France and Germany.
The British campaign featured assertions and allegations tossed around with little regard to the facts. Both sides played to emotion, and the most common emotion played upon was fear.
The “Remain” side, citing scores of experts and elite opinion, warned that leaving the bloc, a so-called Brexit, would mean an economic catastrophe, a plunging pound, higher taxes, more austerity and the loss of jobs.
The Leave side warned that remaining would produce uncontrolled immigration, crime and terrorism, with hordes pouring into Britain from Turkey, a country of 77 million Muslims that borders Syria and Iraq and hopes to join the European Union.
Just a week before the vote, the country was jolted by the brutal murder of a young Labour member of Parliament, Jo Cox, 41, a strong supporter of Remain. A man who prosecutors said shouted “Britain first,” “This is for Britain” and “Keep Britain independent” was charged with her murder.
In England especially, 85 percent of the population of Britain, many people fell back on national pride, cultural exceptionalism and nostalgia. Many English voters chose to believe the insistence of anti-Europe leaders like Boris Johnson, the former mayor of London and potential challenger to Mr. Cameron, that as a great nation, Britain would be more powerful and successful outside the European Union than inside.
In Scotland and Northern Ireland, by contrast, there was a strong pro-Europe feeling that has only increased tensions within the United Kingdom itself.
With this result, those who favor Scottish independence will have a new wind for another referendum, even if they may wish to wait until they are sure to win one.
And Northern Ireland, which has long had an open border with the Republic of Ireland, a member of the European Union, will face a new reality. That open border will become the border between the European Union and a nonmember, and for security and economic reasons it will have to be equipped with border posts to check goods and passports.
Mr. Cameron felt pushed into announcing the referendum in 2013 by the anti-Europe wing of his own party, amplified by concerns among other Tories that U.K. Independence Party and Mr. Farage were cutting too sharply into the Conservative vote.
Still, Mr. Cameron entered the campaign with the force of economic experts, President Obama, European allies and big business behind him. But as ever, referendums are not about the question asked but the political mood at the time, and the political mood is sour.
Mr. Cameron was also, predictably, undermined by the new Labour Party leader, Jeremy Corbyn, a man of the more traditional hard left who was a surprise choice to lead the party after its disastrous defeat in the May 2015 elections.
Mr. Corbyn voted against British membership in what was then the European Economic Community in the last referendum, in 1975. This time, he said he and Labour would support continued membership, but he refused to campaign alongside Mr. Cameron, reluctant to save his political opponent.
And asked how he felt about the European Union, Mr. Corbyn said he was “not a huge fan,” and on a scale of 10, Mr. Corbyn said between “seven and seven and a half.”
While Mr. Cameron emphasized risks to Britain’s economy and security and national influence, Mr. Corbyn talked of protecting workers’ rights.
The victory of the Leave campaign and Britain’s exit from the European Union risks making Britain a poorer place, if the pre-referendum economic forecasts prove true, and hurting many of the very people that Mr. Corbyn represents.