Theresa May said Sir Kim’s departure was “a matter of deep regret” after the ambassador said it was “impossible” for him to continue.
Tory leadership candidate Boris Johnson has faced strong criticism for failing to fully support him.
President Trump said on Monday that the US would not deal with Sir Kim.
The US president had branded him “a very stupid guy” after confidential emails emerged where the ambassador had called his administration “clumsy and inept”.
In a letter to the Foreign Office, Sir Kim said he wanted to end speculation about his position: “The current situation is making it impossible for me to carry out my role as I would like.”
His resignation has prompted widespread support for Sir Kim as well as criticism of Tory frontrunner Boris Johnson.
According to some Whitehall sources, Sir Kim decided to resign after Mr Johnson refused to support him during the Tory leadership debate on Tuesday night, said BBC diplomatic correspondent James Landale.
Mr Johnson was asked repeatedly by fellow leadership candidate Jeremy Hunt whether he would keep Sir Kim in post if he became prime minister, but refused to answer.
It is understood Mr Johnson spoke to Sir Kim on the phone on Wednesday afternoon.
Following Sir Kim’s resignation, Mr Johnson said he was “a superb diplomat” and whoever was responsible for the leak “has done a grave disservice to our civil servants”.
Asked why he was not more supportive of Sir Kim, he said it was “wrong to drag civil servants into the political arena”.
Europe Minister Sir Alan Duncan – who backs Mr Hunt in the leadership contest – said it was “contemptible negligence” of Mr Johnson not to support Sir Kim.
“He’s basically thrown this fantastic diplomat under a bus to serve his own personal interests,” he said.
However, Sir Michael Fallon – a supporter of Mr Johnson – told BBC Newsnight Sir Kim’s position became untenable “long before the debate on Tuesday night” and he understands the ambassador did not watch it.
The backlash against Mr Johnson was “a shabby attempt to politicise” the affair and the leadership contender had “made it clear he supports all our diplomats”, he said.
Labour leader Jeremy Corbyn said Mr Johnson wanted a “sweetheart trade deal” with the US and his lack of support for Sir Kim “shows he won’t stand up to Donald Trump”.
Tory MP and chairman of the Commons’ foreign affairs committee Tom Tugendhat said in a tweet: “Leaders stand up for their men. They encourage them to try and defend them when they fail.”
Fellow Tory leadership candidate and Foreign Secretary Mr Hunt told the BBC Sir Kim was “doing his job” and his resignation was “a black day for British diplomacy”.
This is a truly incredible situation. Sir Kim was one of the UK’s most highly respected diplomats and for him to be forced out like this is a major event.
Traditionally, we’ve had a clear separation between diplomacy and politics, but not now.
A senior government source said to me earlier this week that this country would never let another country decide who represents us abroad – well, that’s what has happened.
Make no mistake, it’s a big convention that’s been broken. And these things matter.
In terms of the leadership race, there’s no doubt that seeing Mr Johnson fail to back him was what changed the dial for Sir Kim and led him to decide he had to quit.
But other sources dispute that claim suggesting it was not the deciding factor and the two men spoke warmly this afternoon.
Mr Johnson hasn’t even moved into No 10 yet – the widely held assumption is that he will – but this is the first major act of a still hypothetical premiership.
And if and when he does become PM, this issue could be something that dogs him for some time, and gives those in the party who are hostile to him ammunition.
Mrs May said Sir Kim had had the full backing of the cabinet and he was owed an “enormous debt of gratitude” for his “lifetime of service” to the UK.
Public servants should be able to give “full and frank advice”, she said, adding that it was important to defend “our values and principles, particularly when they are under pressure”.
Head of the diplomatic service Sir Simon McDonald said it was the first time in his career that a head of state had refused to work with a British ambassador.
He described the leak as “malicious” and told Sir Kim: “You are the best of us.”
President Trump could well wake up this morning thinking he has the power to veto who the UK has as its ambassador.
It wasn’t his more colourful remarks on Twitter that really ended Sir Kim’s time, but Mr Trump’s public announcement that he would no longer work with him.
The effects of that were felt immediately. There was a banquet that Sir Kim was immediately dis-invited from. Next, he couldn’t attend an event with minister Liam Fox.
It was clear he was being frozen out and for an ambassador access is everything. Without it, it’s impossible to do the job.
More broadly, it’s like this… There’s never been parity in the special relationship between the UK and US – it’s never been a relationship of equals but right now it seems particularly lopsided.
The US knows that Britain is fairly isolated right now internationally and needs the US more than ever. Donald Trump has wielded that power mercilessly in this row.
In a letter to Sir Kim, Cabinet Secretary and civil service head Sir Mark Sedwill said that while he understood his reasons for resigning it was “a matter of enormous regret that you were put in this position after a shocking betrayal of trust”.
Cabinet Office Minister David Lidington said he was “enraged” by the situation and morale in the senior ranks of the civil service had taken “a very heavy blow”.
Former head of the civil service Lord O’Donnell told the BBC Sir Kim’s successor could be chosen within two weeks – while Mrs May is still prime minister.
Mr Johnson pictured with Sir Kim in 2017 while he was in Washington DC as foreign secretary
In the emails leaked to the Mail on Sunday, Sir Kim said: “We don’t really believe this administration is going to become substantially more normal; less dysfunctional; less unpredictable; less faction-riven; less diplomatically clumsy and inept.”
The emails, dating from 2017, said rumours of “infighting and chaos” in the White House were mostly true.
The government has opened an internal inquiry into the publication of the memos and police have been urged to open a criminal investigation.
Downing Street confirmed there had been some “initial discussions” with police regarding the leak and if there was concern about criminal activity they would become involved “more formally”.
Metropolitan Police Commissioner Cressida Dick said it was “deeply worrying” diplomatic cables had ended up in the public domain.