They do provide legitimate protection. If bandits or unsavory characters harass the village, the taliban will roll up with AKs and rpgs. They also adjudicate local disputes like "he stole my goat" or "he had sex with my daughter". The afghan military and police forces don't spend a lot of time in the poorest regions of the country, so it's usually the taliban that has to make someone give back the goat or marry the daughter.
In return for protection and dispute resolution, the people in poor afghan villages usually grow poppy on their lands to be sold as opium and volunteer military aged males as taliban recruits. When you think of this system at scale, running unchecked, you can see how 9/11 style acts of terrorism happen. The bigger threat is that a well resourced and uncontrolled taliban in Afghanistan could destabilize the whole region by inciting muslim conflicts.
The US response was close to being appropriate for disrupting a protection racket. In the near term, they sent marines to provide protection for poor villages and actively hunt the taliban. In the long term they tried to introduce free democracy to the country, to provide security and rule of law to the poorest people and put the taliban out of business.
The reason it's taken so long to produce results is optimism on the part of western politicians. Afghanistan is a country that's only loosely held together and there are many factions and families in Kabul competing to grab power in a newly formed government. It could still be decades before things are settled enough that they have time to worry about protecting their poorest citizens, and a strong taliban doesn't pose a direct threat to the most lucrative parts of the government.
It's a complicated situation mostly because the taliban is exploiting people who have little to offer to the government that is supposed to protect them. But many democracies start out this way, and ultimately western action may have helped the situation.