It would be grossly unfair to think that the NATO and US military did not try their very best to win this. Many soldiers paid a very high price and are now justifiably very distressed. Pretty certain however is that the US and NATO governments were careless and overconfident when they engaged in yet another hard-to-win guerilla war.
For 20 years, the US + NATO were involved in a counterinsurgency war. Those are very hard to win for a foreign power, Malaysia(UK)
* was about the only one I can think of after WW2 involving a Western democracy
Some counter-insurgencies that were won, by local troops:
Peru vs Shining Path. 75K deaths, about 25K of those civilians killed by the government.
Spain vs. ETA. UK vs. IRA. More police actions vs. narrow terrorist bases than full-on wars.
Indonesia vs. Communists. Extremely bloody, 500K-1.2M deaths. (quite possibly not a real insurgency but rather a pretext to suppress political opponents by Suharto - txs @timeskull)
Counter-insurgencies that were lost:
- Afghanistan. Soviets lost.
- Vietnam. The US lost.
- Algeria. France lost.
- Vietnam. France lost.
- (almost forgot) Iraq. The US was asked to leave and the present Iraqi government is a liability rather than an asset most of the time, being Iran-aligned, minority-oppressing, and incompetent at dealing with ISIS.
Why are they so hard to win.
Picture yourself as a "good guy" foreign soldier fighting against "bad guys" insurgents. Your goals are to:
protect and bring over to your side the civilians
you don't speak the local language or know the customs
development projects, if successful, can help you
winning over local leaders helps too
having a competent and credible government that the population likes is the gold standard. Don't mess this up (but it's your home politicians' responsibility and you can assume they'll drop the ball).
one strategy calls for establishing safe areas for the population by having dispersed troops in outpost providing local security and then spread out your influence. This however puts you in a static defensive posture and leaves many more-or-less isolated units at risk.
kill bad guys. However, since insurgents are essentially badly armed civilians fighting you, every time you kill one, you may anger his cousin/brother/buddy into becoming an insurgent.
- the bad guys look just like local civilians. be sure not to kill civilians by mistake! Hint: fast aircraft suck at this.
Now, say you are an insurgent, fighting against the foreigners. Your goals are:
kill anyone collaborating with the foreigners and neutralize development projects. Kill any competent local officials. This is why talking about nation-building is easy, but hard to pull off.
- neutralize foreigner-population communication, which is why killing translators is a popular goal
provoke foreigners into killing civilians, preferably with media exposure
keep the population either on your side or too scared to oppose you.
kill foreign soldiers. Not necessarily a huge goal, except it can provoke them to overreact and it's great for morale and propaganda.
The question then becomes whose willpower outlasts the others. If the regular soldiers are not in a do-or-die situation, i.e. if they are foreigners who can return home when their public tires of the war, there is a good chance the insurgents will win.
If the government/outside power can gradually isolate the population from the insurgents' cause, the manpower replacements for the insurgents will wither. Extreme insurgent violence towards civilians can discredit them. Successful governments that win over the people can carry that off as well.
Finally, a minority insurgency that is operating against a government in its own country (ETA, IRA) really leaves the government no choice but to continue the fight or solve via negotiations, as the government can't pull out. The communication problem largely goes away and dead soldiers may turn public opinion against you. Part of the reason why Peru and Indonesia ended up with government wins. This is also the reason Hamas' terrorism is bound to fail: their stance that Israel should not exist backs Israel into a corner.
For the rest, well, standard armies are still trying to figure out a foolproof way to win these wars. Counter-intuitively, you typically need more soldiers than there are badly-armed insurgents to win.
For details on how the US and NATO failed, you might as well read Pete W's linked War Nerd article, it looks pretty solid:
The US government under Rumsfeld
- didn't recognize the importance of nation-building, offering better outcomes to civilians or restraining airpower use early.
- It didn't commit enough troops in the beginning, as Iraq had priority.
- Most of all, starting with Karzai it's been bad government after bad government.
- However I don't agree all that much with that article's conspirational analysis that spending $2.4 trillion was the point, this looks more like an honest error: don't attribute to malice what can be explained by hubris and stupidity.
You also need to consider if you want to "tune" your regular army to be efficient at counter-insurgency: being nice, helping out with development projects, having weaponry useful to that context rather than good at killing peer-enemy field armies. That's why a police force approach is best, if you can pull it off. Also, constant low-level casualties and deployments are a drain on recruitment and retention of career soldiers.
* Malaysia is "pretty special". The Communist insurgents were ethnic Chinese who were distinct from the larger Malay population. And the UK had pretty much promised to decolonize anyway. And see Fizz's comment.
**. Why the post-WW2 cutoff date? Simply put, because Western electorates, over time since decolonization, have become vastly less tolerant of their militaries killing large numbers of people without a critical reason. It is hard to picture modern French people for example accepting the need to kill 300K-1M Algerians to stay in Algeria. Or Americans acquiescing in Rolling Thunder and Linebacker II. See also Fizz and Graham's comments below.
*** Under Rumsfeld? As Secretary of Defense, Rumsfeld had a huge, outsized, influence on Afghanistan/Iraq. In Iraq at least, he managed to sideline the State Department after the initial successful invasion to decide how the US should interact with Iraq.