Afghanistan had been in a state of almost constant war for 20 years even before the US invaded.
In 1979 , a year after a coup, the Soviet army invaded Afghanistan to support its communist government. It fought a resistance movement - known as the mujahideen - that was supported by the US, Pakistan, China and Saudi Arabia, among other countries.
In 1989 , Soviet troops withdrew but the civil war continued. In the chaos that followed, the Taliban (which means "students" in the Pashto language) sprang up.
They first rose to prominence in the border area of northern Pakistan and south-west Afghanistan in 1994 . They promised to fight corruption and improve security and, at that time, many Afghans were tired of the excesses and infighting of the mujahideen during the civil war.

It's thought the Taliban first appeared in religious schools, mostly funded by Saudi Arabia, which preached a hardline form of Islam.
They enforced their own austere version of Sharia, or Islamic law, and introduced brutal punishments. Men were made to grow beards and women had to wear the all-covering burka.
The Taliban banned television, music and cinema and disapproved of girls' education.

And because the Taliban gave shelter to militants from the al-Qaeda group, it made them an immediate target for an attack by US, Afghan and international forces in the wake of 9/11.

Why has the war lasted so long?

There are many reasons for this. But they include a combination of fierce Taliban resistance, the limitations of Afghan forces and governance, and other countries' reluctance to keep their troops for longer in Afghanistan.

At times over the past 18 years, the Taliban have been on the back foot. In late 2009, US President Barack Obama announced a troop "surge" that saw the number of American soldiers in Afghanistan top 100,000.
The surge helped drive the Taliban out of parts of southern Afghanistan, but it was never destined to last for years.
As a result, the Taliban were able to regroup. When international forces withdrew from fighting, Afghan forces left to lead the charge were easily overwhelmed. To make matters worse, Afghanistan's government, that is full of tribal division, is often hamstrung.

The BBC World Service's Dawood Azami says there are five main reasons the war is still going on now . They include:
  • a lack of political clarity since the invasion began, and questions about the effectiveness of the US strategy over the past 18 years;
  • the fact each side is trying to break what has become a stalemate - and that the Taliban have been trying maximise their leverage during peace negotiations
  • an increase in violence by Islamic State militants in Afghanistan - they've been behind some of the bloodiest attacks recently
There's also the role played by Afghanistan's neighbour, Pakistan.
There's no question the Taliban have their roots in Pakistan, and that they were able to regroup there during the US invasion. But Pakistan has denied helping or protecting them - even as the US demanded it do more to fight militants.

How have the Taliban managed to stay so strong?

The group could be making as much as $1.5bn (£1.2bn) a year , a huge increase even within the past decade. Some of this is through drugs - Afghanistan is the world's largest opium producer, and most opium poppies - used for heroin - are grown in Taliban-held areas.

But the Taliban also make money by taxing people who travel through their territory, and through businesses like telecommunications, electricity and minerals.
Foreign countries, including Pakistan and Iran, have denied funding them, but private citizens from the region are thought to have done so.

How costly has the war been?

Extremely.
It's difficult to say how many Afghan troops have died - the numbers are no longer published. But in January 2019, Afghan President Ashraf Ghani said 45,000 members of the security forces had been killed since 2014 .
Nearly 3,500 members of the international coalition forces have died since the 2001 invasion, more than 2,300 of them American.
The figures for Afghan civilians are more difficult to quantify. A UN report in February 2019 said more than 32,000 civilians had died. The Watson Institute at Brown University says 42,000 opposition fighters have died.
The same institute says conflicts in Iraq, Syria, Afghanistan and Pakistan have cost the US $5.9 trillion since 2001.
The US is still conducting air strikes against the Taliban, instigated by the third president to oversee the war, Donald Trump. But he is keen to reduce troop numbers before he faces another election in November 2020.
Many in Washington and elsewhere fear that a full US troop pull-out would leave a vacuum that could be filled by militant groups seeking to plot attacks in the West.
The Afghan people, meanwhile, continue to bear the brunt of the long and bloody conflict.