The PM said there could "never be a perfect moment" to withdraw, but it was "never intended to be permanent".
More than 450 British troops have died during the conflict with the Taliban and fighters from al-Qaeda since 2001.
But Britain's most senior general has warned Afghanistan could slide into civil war once foreign troops leave.
The US has said it will withdraw all forces by 11 September.
The US-led bombing of Afghanistan began in October 2001, following the 11 September attacks on the United States.
At the height of the war, Nato had more than 130,000 troops from 50 nations in Afghanistan. The UK had 9,500 personnel and 137 bases in Helmand province alone.
In a statement to the House of Commons, Mr Johnson said after the majority of troops returned home in 2014, about 750 service personnel stayed in Afghanistan under Nato's mission to train and assist the country's security forces.
"No-one should doubt the gains of the last 20 years," he told MPs. "But nor can we shrink from the hard reality of the situation today."
He said the situation in Afghanistan now is "very different" compared to 20 years ago when the country was "the epicentre of global terrorism".
"We and our Nato allies were always going to withdraw our forces," Mr Johnson said. "The only question was when, and there could never be a perfect moment."
US President Joe Biden announced in April that American troops will leave Afghanistan by 11 September, saying it was "time to end America's longest war".
The US had some 2,500 troops in the country as part of a 9,600-strong Nato mission.
That would coincide with the 20th anniversary of the terror attacks on the World Trade Center and the Pentagon in the US in 2001.
The Taliban told the BBC this month that any foreign troops left in Afghanistan after Nato's September withdrawal deadline will be at risk as occupiers.
But violence in the country continues to rise, with the Taliban taking more territory.
Under a deal with the militant group, the US and its Nato allies agreed to withdraw all troops in return for a commitment by the Taliban not to allow al-Qaeda or any other extremist group to operate in the areas they control.
After nearly 20 years of western military support, most Nato forces have now left the country for good.
That includes the majority of the 750 British troops who were in the capital Kabul to assist with security and training.
The head of the Armed Forces, General Sir Nick Carter, says that those British troops who served in Afghanistan can be proud of what they achieved.
But even he describes the current security situation in Afghanistan as "pretty grim", with the Taliban making significant advances.
General Carter says it's "plausible" that the country might now fracture along tribal and ethnic lines.
Though he still believes the Taliban will not be able to control the whole country.
A total of 475 British military personnel lost their lives in the country - most killed in Helmand before combat operations ended in 2014.
Their families will question whether they really leave a lasting legacy - whether their sacrifice was in vain.
Boris Johnson insists that Britain is not walking away from the country.
There will still be a very small British military presence in Kabul to protect diplomatic missions. But did this costly Nato mission really achieve what it set out to do?
Labour's deputy leader Angela Rayner said the Taliban were "making gains on the ground" and "serious questions remain about the future stability of Afghanistan".
"A security threat remains to the wider world including to the UK, and nobody wants to see British troops permanently stationed in Afghanistan, but we simply cannot wash our hands or walk away," she said.
There had been "moments of huge difficulty" in the last 20 years, Ms Rayner said, but Afghanistan's current situation "is more concerning than at any other point in many years".
"It's hard to see a future without bloodier conflict and wider Taliban control," she said.
Mr Johnson told MPs Britain was "not walking away" from Afghanistan.
"We are keeping our embassy in Kabul and we will continue to work with our friends and allies, particularly with the government of Pakistan, to try to bring a settlement, to try to ensure that the Taliban understand that there can be no military path to victory and there must be a negotiated solution," he said.
Speaking after Mr Johnson's announcement, Head of the Armed Forces General Nick Carter said the situation in Afghanistan was "pretty grim", with half the country's rural districts now in the hands of the Taliban.
He warned there was a danger of "state collapse", adding: "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."
Designated transnational terrorist group, al-Qaeda, was able to establish itself in Afghanistan, led by Osama Bin Laden, over five years from 1996-2001.
It set up terrorist training camps, including experimenting with poison gas on dogs, and recruited and trained an estimated 20,000 jihadist volunteers from around the world. It also directed the twin attacks on the US embassies in Kenya and Tanzania in 1998, killing 224 people, mostly African civilians.
Al-Qaeda was protected by the government at the time, the Taliban, who refused calls from the international community to expel the terror group after the 9/11 attacks in the US in September 2001. This led to the US-led bombing of Afghanistan, which began in October 2001.