Conservative prime minister resigns after Britain votes for Brexit from the EU in its historic referendum.

His voice cracked at the end of his farewell speech. It wasn't supposed to end like this. David Cameron was the reforming modern Tory who led his party out of the wilderness and into government in two consecutive elections, preventing the break up of the United Kingdom along the way by fighting off the Scottish nationalists in an independence referendum.

And yet there he was, resigning in front of the nation, a casualty of the final battle in a political war that has raged among Conservatives for several decades, a decisive battle he started believing he could win with relative ease.

Cameron: healer of economies, healer of old wounds. But as the Ancient Greeks understood, hubris always gets you in the end. Many in his party wanted a Brexit more than they wanted him. And so did most of the country.

Britain will leave the European Union (EU) and Cameron will leave Downing Street, though he will stay on as a caretaker of his party as it searches for a new leader, the guiding light who will now have to head negotiations on the country's future relationship with Europe. Trade, borders, security, scientific research — everything is back on the table for discussion.

So what of Cameron's legacy? His achievements will, of course, be overshadowed by his biggest failure: not convincing the British public that they were better off inside the EU than out. But he was also a Tory moderate who cut taxes, for both the poorest workers and the biggest businesses.

He was a social liberal who fought against the conservatism of his own benches to push gay marriage through the Commons. He led Britain to become the fastest-growing western economy. He won the Conservatives their first parliamentary majority in two decades at the 2015 general election, against the odds.

David Cameron and his wife Samantha leave a polling station after voting in the local council and European electionsReuters

But he was also attacked by his many critics for what they said were brutal and unnecessary public spending cuts, such as welfare reforms that left some of the poorest and most vulnerable financially worse off, some of whom turned to food banks. He was accused of cutting tax for the richest in society while making the poor pay through austerity.

His judgement was called into question over the appointment of Andy Coulson as his head of communications, who later left the government in disgrace over his role in the tabloid phone hacking scandal, for which he was imprisoned. Moreover, Cameron's intervention alongside Nato in Libya is widely seen as a disaster.

And what was one of his biggest achievements — preserving the union and deterring Scottish independence — is now under threat once again because of the vote to leave the EU, which Scotland's public voted convincingly to remain in. The nationalists are baying for a second independence referendum, which they would probably win. Cameron now faces becoming the man who broke not just one union, but two.

The Europe issue predated by many years Cameron's leadership of the Conservative party, which he secured in 2005 by defeating rival David Davis. He made eurosceptic noises during his ascent in the party through the 1990s, which helped him take the Commons seat of Witney in 2001, though he was never a committed europhobe like so many other Conservatives. He worked for the party machine as Europe tore apart both the Thatcher and Major governments so he understood well the damage it could do.

The referendum issue dogged Labour prime minister Tony Blair, who nearly had to hold one — and looked like he would lose it — on the European constitution in 2006. Its rejection by other EU member states spared his blushes. But on the right of politics, the UK's membership of the EU has always been in question.

At a low point of the Cameron government, when the prime minister was weakest, the Tory eurosceptics made their move. When the economy looked to be tanking; when there had been the notorious "omnishambles" budget; when Ukip were beginning to attract voters from the Conservatives (and even a couple of defecting MPs, Douglas Carswell and Mark Reckless); Cameron was forced to appease the many eurosceptics agitating against his leadership. Cornered, he gave them the referendum they craved. But he believed he could win it anyway.

Immigration is a cause for serious concern among British voters. They are worried about integration, wages and the pressure on public services. So a large portion of the blame for Brexit lay with Cameron's pledge to voters that he would get net migration down to the tens of thousands a year. He never got near it, of course, because you cannot control migration from the EU. That is not a flaw of the EU. Free movement is part of the bargain.

Prime Minister David Cameron greets German Chancellor Angela Merkel at Number 10 Downing Street in LondonReuters

The migration pledge would be a catastrophic strategic blunder, further eroding what little trust there was left in politicians and the establishment, and handed Nigel Farage's Ukip a giant rhetorical stick with which to constantly beat the government. Cameron was dealt another blow during his attempt to renegotiate Britain's membership of the EU as a means of placating undecided voters and soft eurosceptics.

His friend and European ally Angela Merkel, the German chancellor, would not agree to temporary migration curbs branded the "emergency brake". This seriously undermined the credibility of his reform package, which was hard-won from fellow EU leaders and had genuine significance. The reforms were not enough; just a worthless scrap of paper, chided leave campaigners.

It was a bitter, divisive, nasty referendum campaign. Elements of the movement to leave played up tensions between natives and migrants, and happily misled voters on the now infamous £350m a week UK's EU contribution figure and Turkey's protracted attempts to become a member of the EU. But Cameron also played his part on the remain side by cranking up the scaremongering. Brexit would mean World War 3, he said. Isis would be happy with a Brexit. Worse still, house prices might fall.

Oh, and listen to the experts — the IMF, the OECD, Nato, economists, banks, and so on — who say the UK should remain in the EU. But there is an obvious problem with appealing to the authority of the establishment when nobody trusts elites anymore. And that lack of faith in the wisdom of philosopher kings was borne out in the referendum.

As he grew up in the upper-class ruraltopia of Berkshire, in the old rectory he called home; as he walked among the cloisters and spires of Eton College then Oxford; Cameron must have thought about all the great men of history who came before him through these same institutions and considered his own future, dreaming of what his role might be in the story of the United Kingdom. And he has his place in history. But it's probably not the one he was hoping for.