Gordon Brown tonight announced his resignation as Prime Minister.
Mr Brown said he had notified the Queen's private secretary he was advising that she should appoint David Cameron as the new premier.
Mr Brown, his voice cracking with emotion, also said he was resigning immediately as Labour leader.
He said it had been "a privilege to serve" and wished his successor well.
Mr Brown then left Downing Street for Buckingham Palace holding hands with his wife Sarah and sons John and Fraser.
With his wife Sarah standing by his side on the steps of No 10, he said: "My constitutional duty is to ensure that a government can be formed after last week's general election.
"I have informed the Queen's private secretary that it is my intention to tender my resignation to the Queen.
"If the Queen accepts, I shall advise her to invite the Leader of the Opposition to seek to form a government."
Mr Brown said: "I wish the next prime minister well as he makes the important choices for the future.
"Only those who have held the office of prime minister can understand the full weight of its responsibilities and its great capacity for good.
"I have been privileged to learn much about the very best in human nature and a fair amount too about its frailties - including my own."
Mr Brown said he had "loved the job, not for its prestige, its titles and its ceremony, which I do not love at all.
"No, I loved the job for its potential to make this country I love fairer, more tolerant, more green, more democratic, more prosperous, more just - truly a greater Britain."
He also paid tribute to the armed forces, saying: "Now that the political season is over, let me stress that having shaken their hands and looked into their eyes, our troops represent all that is best in our country and I will never forget all those who have died in honour and whose families today live in grief."
He paid tribute to colleagues and staff, who had been "friends as well as brilliant servants of the country".
Minutes after Mr Brown left Downing Street, a few hundred yards away talks ended between Liberal Democrat and Tory negotiators on a future government.
Shadow foreign secretary William Hague said: "We now have some recommendations to take back to David Cameron and our parliamentary colleagues."
Lib Dem leader Nick Clegg's chief of staff Danny Alexander said that the talks had taken place in a "good atmosphere".
Mr Alexander added: "We are returning to report back to Nick Clegg and our parliamentary party."
The talks in the Cabinet Office had lasted five and a half hours.
In his resignation speech, Mr Brown paid tribute to Sarah's "unwavering support and her love" and to his sons - John and Fraser - "for the love they bring to our lives".
He was leaving, he said, "the second most important job I could ever hold" after that of being a husband and father, which he would now "cherish even more".
Concluding "Thank you, and goodbye", he then turned back to the door of No 10 from where the two boys joined the couple, posing for photographs before walking, all holding hands, down the street to cheers.
Mr Brown's dramatic resignation came a day after he announced he would step down as Labour leader in the wake of the party's defeat in last Thursday's General Election.
It appeared overnight that Labour might be able to forge a deal with Liberal Democrats to create a "progressive alliance", but talks this morning broke down after about three hours.
It was later announced that Mr Clegg had been in face-to-face talks with Mr Cameron at the same time as his negotiation team was talking to Labour.
The Lib Dems and Conservatives returned to talks at the Cabinet Office at around 2pm, and as the day wore on it appeared ever more likely that they would reach some sort of deal which would put Mr Cameron in 10 Downing Street at the head of a coalition government.
Mr Brown left the Palace after around 20 minutes and was driven away.
After leaving the Cabinet Office talks, both Mr Hague and Mr Alexander said separately that they were "pleased with our discussions".
Mr Brown has told friends he plans to resign as an MP and quit politics altogether following the dramatic events of the past few days, sources told the Press Association.
Close friends of the former Prime Minister said they expected Mr Brown to concentrate on charity work.
He has no plans to stay in politics or follow Tony Blair's example of taking up lucrative business work, said the sources.
But there was also speculation that Mr Brown will be approached by international finance organisations keen to use his experience.
Mr Brown then made the short journey to Labour Party headquarters, arriving at 7.50pm.
Staff and fellow MPs applauded as he climbed out of the car.
Deputy party leader Harriet Harman, Energy Secretary Ed Miliband, Work and Pensions Secretary Yvette Cooper, Transport Secretary Lord Adonis and International Development Secretary Douglas Alexander were among those who greeted him.
Ms Cooper hugged Mrs Brown as she followed her husband into the building.
A Liberal Democrat spokesman said: "It is clear that the Labour Party never took seriously the prospects of forming a progressive, reforming government with the Liberal Democrats.
"Key members of Labour's negotiating team gave every impression of wanting the process to fail and Labour made no attempt at all to agree a common approach with the Liberal Democrats on issues such as fairer schools funding for the most deprived pupils and taking those on low incomes out of tax.
"It became clear to the Liberal Democrats that certain key Labour Cabinet ministers were determined to undermine any agreement by holding out on policy issues and suggesting that Labour would not deliver on proportional representation and might not marshal the votes to secure even the most modest form of electoral reform.
"It is clear that some people in the Labour Party see opposition as a more attractive alternative to the challenges of creating a progressive, reforming government, not least in the context of a Labour leadership election campaign."
The now familiar lectern appeared in the middle of Downing Street at 7.07pm.
A crowd of aides filed from Number 11 to watch.
The photographers took their positions atop ladders and scaffolds - and at 7.19pm Mr Brown walked from Number 10.
The ranks of the media had swelled in the afternoon as it slowly became clear Mr Brown was likely to resign.
It had started as a day of uncertainty, and when the Labour negotiating team, including Lord Mandelson, Harriet Harman and Ed Balls, left the street at 10am today there was still a spring in their step.
Throughout the day there were more comings and goings from the ministers, and they would soon be gone for good.
The questions shouted by journalists became increasingly aggressive.
"How are the negotiations going?"
"Is there any chance of doing a deal with the Lib Dems now?"
"Is the deal finished?"
"Is the deal dead?"
The answers became less positive. Earlier Ed Miliband described the talks as "constructive".
Later, when Mr Balls was asked if the deal was dead, he replied simply: "We will see."
Lord Mandelson hopped in and out of his Jaguar as he went back and forth.
There may have been a smile on his face but he surely knew his fate.
In the afternoon there were reports that large black holdalls were being loaded into a car at the side of Downing Street.
In reality it may have had nothing to do with the Browns and the day's political machinations, but it nevertheless added to the feeling that he was about to pack his bags and go.
Once again the heavyweight political journalists were out in force.
When Sky's Adam Boulton arrived he received a mocking cheer from the media pack after his bust up with Alastair Campbell yesterday.
But it was Mr Brown's blood that could be smelled tonight.
In the end it was a heart-felt speech of resignation.
Mr Brown looked more at ease than he has done for many days.