"Article 5 provides that if a NATO Ally is the victim of an armed attack, each and every other member of the Alliance will consider this act of violence as an armed attack against all members and will take the actions it deems necessary to assist the Ally attacked."
So, the 9/11 attacks were considered an armed attack on the United States, and the alliance deemed it necessary to overthrow the Taliban in order to prevent Al-Qaeda from perpetrating similar attacks again.
One can disagree with the rationale behind the decision, or the cost-benefit ratio. But it did follow the protocols of the alliance.
... is a very different thing than ...
> occupying a foreign country for 20 years
When NATO invaded the towers were still smoking, the war-on-terror intelligence apparatus had yet to be built, and we (I) had no idea whether another attack was forthcoming. It felt pretty damn reasonable at the time.
Currently, as NATO members, they are not likely to be attacked, but if NATO declares war then that deterrent is gone.
Hopefully not a likely scenario, but who knows.. There's been a lot of tension lately, especially with the likes of Belarus bussing migrants across the border into Lithuania etc, but no idea where this will end.