The situation in Afghanistan is far more complex than it seems to the normal eye. To understand the present we have to look at its history of conflict and war.
In 1979 the Soviet Union invaded Afghanistan to suppress rebel groups that could have ousted the puppet dictatorship. After the invasion, several young men from the Middle East started joining the rebel groups and formed the mujahedeen. The men who joined the mujahedeen considered it to be a religious struggle and many of the recruits had extremist views. The mujahedeen with the help of funding and support from the USA and its allies were able to defeat the Soviets and the Soviets withdrew in 1989.
After ousting the Soviet Union and the dictatorship Afghanistan descended into a bloody civil war. Seeing this power vacuum Mullah Amar and his followers made the Taliban and in 1994 they started gaining support. The Taliban promised to end the era of anarchy and the civil war which they believed was caused by foreign powers and the greed of mujahedeen leaders. Taliban also used another tool that was far more effective in gaining support which was religion. Mullah Omar and the Taliban started spreading a narrative that promoted the idea that the Afghans had forgotten about their religion and were hiding it. The Taliban started gaining momentum in 1995 and in 1996 they took over Kabul and declared an official government. After the takeover, Mullah Omar announced that a pure Islamic government will rule over Afghanistan.
In the late 1990s, the Taliban’s barbaric style of governance was brought to international attention. Human Rights activists and countries from all over the world expressed their concerns about the Taliban’s treatment of the general population, especially women. Most importantly, the Taliban was also responsible for harboring Al Qaeda and Osama Bin Laden. After the terrorist attacks of September 11, 2001 (9/11) the US decided to wage a full blown war against terror and the Bush administration had made it clear that those harboring terrorists would be treated as terrorists. The US launched operation enduring freedom and invaded Afghanistan in October 2001. The American Army with the help of Afghan opposition groups which most notably included the Northern Alliance was able to defeat the Taliban. Following the defeat, the Taliban retreated and many of its members fled to the remote villages of Afghanistan or to Neighbouring Pakistan.
For the first few years, the Taliban were dormant and provided only pockets of resistance to the democratic government established by the US in Kabul. The US after years of war started rebuilding Afghanistan and the most important sector was roads as Afghanistan lacked roads and to prevent a future Taliban resurgence all parts of the country had to be connected. At the forefront of this rebuilding project was the ring road, a highway which would be a 3500 km loop connecting Kabul, Kandahar, Heart, and Mazar-i-Sharif the four biggest cities of Afghanistan. The rig road was partially built by the Soviets in the 1960s but was ruined by years of war. The Kabul to Kandahar stretch of the highway cost over 200 million dollars and over 100 lives were lost to the pockets of Taliban resistance. Over the next couple of years, the international community pledged 1.5 billion dollars to build the ring road. But, The US did not finish the job as they invaded Iraq in 2003 and they diverted their funding and resources over there. Nonetheless, The Afghan government was provided billions of dollars in foreign aid over the years. At the time Afghanistan was a fragile and corrupt democracy. Billions of dollars were lost to corruption scandals, Afghan politicians were caught laundering money to offshore accounts in Dubai. Corruption was so common that in 2011 a survey by the ASIA foundation revealed that Afghans believed that corruption was their third biggest problem after insecurity and unemployment. This large scale corruption hindered the rebuilding process and gave the Taliban time and resources to regroup.
Even before 2017 Taliban had taken over a large chunk of the Afghan countryside. There were frequent attacks in Kabul and clashes between the Taliban and the military were common. The fall of the government seemed imminent.