There’s nothing unique about Afghanistan that means that Al Qaeda can plot attacks from Afghanistan and no where else in the world. (Indeed, a significant portion of 9/11 seems to have been plotted in Germany). Even Stephen Biddle — who strongly advocates for the US to remain at war in Afghanistan — admits that preventing Al Qaeda from having a sanctuary in Afghanistan isn’t a very sensible argument.
It is certainly true that terrorists don't need a haven like Afghanistan in order to carry out attacks. But it does make their lives a ton easier. Part of this goes back to some of my early observations about statecentrism and how it can distort our outlook on the problem of terrorism. The problem is less friendly versus unfriendly states than it is functional versus dysfunctional states. An unfriendly but functional state (such as Iran) can still be dealt with via the normal tools of statecraft, which is why I don't think they're as significant a threat in terms of spawning terror towards America as they are sometimes labeled. Places like Afghanistan (and, say, Somalia), which either have collapsed or are at risk for doing the same, don't provide that option, and that's dangerous.
It's always better to at least have the option of domestic enforcement as a check against the growth of terror infrastructure. That doesn't mean it will always work, but it is nearly always better than not having it. In this respect, trying to convert Afghanistan into a stable, functional state with control over its borders and a monopoly on violence is almost definitely reasonably related to the fight against terrorism.