Contentious, as Afghanistan (or the Taliban government in charge at the time) didn't attack the US.
A Saudi Arabian national plotted it, and the terrorists who carried it out were from Saudi, UAE, Lebanon and Egypt.
They used Afghanistan as a staging ground for their training camps.
In 20 years the US spent up to 2 trillion dollars there. We spent 80 billion. They didn't really need us, and the people who bombed London in 2005 cited the UKs involvement in Afghanistan and Iraq as the cause.
Unfortunately we won't ever know what things would have been like if we had said no.