US did three big mistakes in Afghanistan - one for each decade, starting with 1980, two before 9/11 and the third after invading the country.
The first mistake was done in '80, during the war against Soviets, when US decided to back Afghan Resistance in arms and finance, but throughout Pakistani secret service (ISI), which choose the destination of the money. Islamabad opted from the beginning for Hezb Islami group, lead by Gulbuddin Hekmatyar, one of de most radical and unscrupulous mujahedeen leaders, who received the largest part of aids. At that time, for Washington there was irrelevant how much fanatic were those who had been fighting against Soviets. And they did the second big mistake: after defeating USSR and withdrawing troops from Afghanistan, Americans have lost their interest for this country and stopped financing. If they had put pressure on Pakistanis and Massoud or other moderates had been supported, situation would have evolved differently.
After Mohammad Najibullah fall, Mujahedeens intended to create a government coalition, but Hekmatyar, backed by Pakistan, tried to take the power in 1992, and the Civil War begun. Conditions have been created for arriving Taliban, who were welcomed cheerily by the population, who believed that they would bring safety and order.
Hekmatyar was defeated and Taliban have again received support from Pakistan and Osama Bin Laden provided them with an Al Qaida brigade. The most part of the country was conquered, but Massoud has resisted in his Panjshir fortress.
After 9/11, Islamabad had to be more discrete about its relations with Taliban, because its partnership with US and American pressure. Nevertheless, Pakistan involvement in the war against terrorism has been only partial, as Islamabad carried on its support for some of Taliban. Consequently, after being defeated by Americans, Taliban found refuge in Pakistan and started reorganizing. When they re-conquered Afghanistan, Pakistani civil premier, Imran Khan, expressed publically his contentment.
The third mistake was the US and NATO retreat beginning with 2014, when Western forces have recognised, actually, that the war in Afghanistan was lost. The stress was put on the state consolidation and Afghan security forces training. But in 2021, all these efforts have proved vain and Kabul has easily fallen.