ANTI-ESTABLISHMENT
Quitting the EU could cost Britain access to the EU's trade barrier-free single market and mean it must seek new trade accords with countries around the world. President Barack Obama says it would be at the "back of a queue" for a U.S. pact.
The EU for its part must absorb the blow at a time when it is struggling to climb out of financial crisis, deal with unprecedented migration and respond to a resurgent Russia.
It faces the loss not only of its most free-market proponent but also a U.N. Security Council veto and one of its most powerful armies. In one go, the bloc will lose around a sixth of its total economic output.
Cameron is expected to formally report the result to his European counterparts within days.
The British leader called the referendum in 2013 in a bid to head off pressure from domestic eurosceptics, above all within his own party. Initially billed as an easy ride, the vote will now be his likely political epitaph. Party rival Boris Johnson, the former London mayor who became the most recognisable face of the "leave" camp, is now widely tipped to seek his job.
In the end, the pro-EU camp was powerless to stop a tide of anti-establishment feeling and disenchantment with a Europe that many Britons see as remote, bureaucratic and mired in permanent crises.
Britain, which joined the then European Economic Community (EEC) in 1973, has always been an ambivalent member. A firm supporter of free trade, tearing down internal economic barriers and expanding the EU to take in ex-communist eastern states, it opted out of joining the euro single currency or the Schengen border-free zone.
Cameron's ruling Conservatives in particular have risked being torn apart by euroscepticism for generations.
World leaders including Obama, Chinese President Xi Jinping, German Chancellor Angela Merkel, NATO and Commonwealth governments had all urged a "Remain" vote, saying Britain would be stronger and more influential in the EU than outside.
The four-month campaign was among the divisive ever waged in Britain, with accusations of lying and scare-mongering on both sides and rows on immigration which critics said at times unleashed overt racism.
A pro-EU member of parliament was stabbed and shot to death in the street a week ago by an attacker who later told a court his name was "Death to traitors, freedom for Britain".
The campaign also revealed deeper splits in British society, with the pro-Brexit side drawing support from millions of voters who felt left behind by globalisation and believed they saw no benefits from Britain's ethnic diversity and free-market economy.
Older voters backed Brexit; the young mainly wanted to stay in.
"People are concerned about how they have been treated with austerity and how their wages have been frozen for about seven years," said John McDonnell, finance spokesman for the opposition Labour Party, which had favoured a Remain vote.
"A lot of people's grievances have come out and we have got to start listening to them."