In a Commons statement, the Prime Minister paid tribute to the "valour and sacrifice" of the British troops who had served in the long struggle against the Taliban.
He said most of the remaining 750 UK military trainers with the NATO mission have already left the country.
The move follows the announcement in April by President Joe Biden that he would withdraw the remaining US forces by 11 September, the 20th anniversary of the 9/11 attacks, effectively ending international military involvement in Afghanistan.
With fears that the departure of foreign troops could lead to a Taliban takeover, Mr Johnson said the UK will continue to support the government of President Ashraf Ghani with more than £100m in development aid and £58m for the Afghan security forces.
"We are not about to turn away nor are we under any illusions about the perils of today's situation and what may lie ahead," he told MPs.
"We shall use every diplomatic and humanitarian lever to support Afghanistan's development and stability.
Watch: UK accused of 'abandoning' Afghanistan to Taliban with withdrawal.
"Of course we will continue to work alongside our Afghan partners against the terrorist threat."
The Ministry of Defence (MOD) said a "small number" of UK military personnel will stay on temporarily to support the transition to the new phase of UK support to Afghanistan.
Head of the Armed Forces General Sir Nick Carter acknowledged the situation in Afghanistan is "pretty grim", with half the country's rural districts now in the hands of the Taliban.
In a briefing for journalists, he said the Afghan defence forces are having to adjust their tactics following the withdrawal of international air support which had previously given them the edge in the fighting.
He said there is now a danger of "state collapse" – with the country fracturing in the way that it did in the 1990s – although he added he doubts the Taliban are strong enough to gain complete control.
"That's where you would see a culture of warlord-ism, and you might see some of the important institutions, like the security forces, fracturing along ethnic or, for that matter, tribal lines," he said.
"If that were to happen, I guess the Taliban would control part of the country, but of course they wouldn't control all of the country."
Watch: Will the Afghanistan withdrawal become a 'complete strategic failure'?
Gen Sir Nick said a "more hopeful scenario" is for the Afghan government to hang on long enough for the two sides to realise they need to reach a political accommodation.
"It is entirely possible that the Afghan government defeats the Taliban for long enough for the Taliban to realise that they have to talk," he said.
"I think the Taliban recognise that they can’t rule all of Afghanistan without a compromise."
Despite the losses suffered during the campaign – totalling 457 members of the UK Armed Forces – Gen Sir Nick said it had changed the lives of millions of Afghans for the better, while there has been no international terrorist attack launched from the country since the original 9/11 attacks on New York and Washington.
He said British veterans who served there can "hold their heads up very high" having demonstrated their "tactical excellence" on the battlefield.
"They were never defeated on the battlefield," he said.
"What they did is to instil inside the British Army, and our British military more broadly, a combat ethos that I don't think we've seen in the British military for some generations.
"That is something that we take away from this campaign with great pride."