So with that short answer out of the way, we can look at the reasoning the leads to that answer.
The United States is in Afghanistan largely to prevent anti-American groups from using the fact that the area is weakly controlled by any central government to use that freedom from oversight to amass resources that could be used to harm the US.
The resources Al-Qaeda was able to amass in Afghanistan were less the money than the central networking ability and the ideological development that could be used elsewhere.
Central networking means that al Qaeda was able to accumulate lists of people that they trust to be reliably ideologically motivated and also trained and disciplined. It is very difficult to put that together in a territory under hostile control. Efforts to take raw ideologically motivated prospective members and weed out those unable to become effective fighters while also training and indoctrinating the rest into fighters are easily thwarted in the US, Europe or anywhere that has a ruling culture that is not accomodating to the ideology being instilled. From Afghanistan, Al Qaeda was able to weed, train and keep track of fighters who could later be coordinated in Europe or the United States.
Central networking played a very important role in 9/11. It is often said that 9/11 was planned in Europe, which it was, but the planners could not have trained, met and trusted each other without Afghanistan or something like it.
Far less important than central networking, but maybe worth a mention, is that Afghanistan also provide a place for Al Qaeda to develop ideologically. An ideological movement is about not only fighting, but needs constant refinement of its world-view in light of outside developments as well as slowing and combating the natural process by which different people's understandings of any ideology drift in different directions over time.
A single source that is capable of holding a party line requires that those who think about how their ideology applies to changes in outside events and differences of interpretation by members are able to be fed, informed of events and kept in communication with the rest of the membership. These functions, if provided in an organized way, are easily broken up in hostile territory, where ideologists can be made to focus on personally avoiding imprisonment rather than the wider implications of their world-view.
Not only could central Al Qaeda have not accomplished the role it played in 9/11 from Europe or the United States. It also could not have played the role it did from Egypt or Saudi Arabia. Egypt and Saudi Arabia do and must break up organizations that would provide central networking or ideological development to advance world-views in conflict with the United States or West as those countries have rulerships that are infamously among their own people known to be subservient to the United States.
So Afghanistan played an important role in Al Qaeda attacks on the West. At this point we have to understand why Al Qaeda's ideology demands conflict with the West. Fortunately, despite the insistence of Western supporters of Israel, Al Qaeda has been consistently explaining the basis of its dispute with the West almost since it was founded.
(Osama Bin Laden’s Nov 2001 interview with DAWN)
The American people should remember that they pay taxes to their government, they elect their president, their government manufactures arms and gives them to Israel and Israel uses them to massacre Palestinians. The American Congress endorses all government measures and this proves that the entire America is responsible for the atrocities perpetrated against Muslims. The entire America, because they elect the Congress.(1997 interview Osama Bin Laden gave to CNN’s Peter Arnett)
REPORTER: Mr. Bin Ladin, you’ve declared a jihad against the United States. Can you tell us why? And is the jihad directed against the US government or the United States’ troops in Arabia? What about US civilians in Arabia or the people of the United States?The United States, in its expensive quest to ensure that the Middle East remains safe for a Jewish state, regularly commits and supports atrocities against the non-Jews of the Middle East. This is a difficult concept for Americans to understand, especially because the people Americans take as sources for news in the Middle East tend to be disposed to minimize the cost of protecting Israel. Americans will have to trust me that the concept is quite easy to understand in the Middle East.
BIN LADIN: We declared jihad against the US government, because the US government is unjust, criminal and tyrannical. It has committed acts that are extremely unjust, hideous and criminal whether directly or through its support of the Israeli occupation of the Prophet’s Night Travel Land (Palestine). And we believe the US is directly responsible for those who were killed in Palestine, Lebanon and Iraq. The mention of the US reminds us before everything else of those innocent children who were dismembered, their heads and arms cut off in the recent explosion that took place in Qana (in Lebanon). This US government abandoned even humanitarian feelings by these hideous crimes. It transgressed all bounds and behaved in a way not witnessed before by any power or any imperialist power in the world.
There are many parts of Africa and South America that could play the role Afghanistan played, but there is no issue that generates anti-Western anger in those regions the way Israel does so in the Middle East. Because of that, the United States is spared the expense of sending tens- or hundreds- of thousands of troops to occupy, for example, lawless areas in Bolivia, Brazil, Burkina Faso or the Congo.
The United States is in Afghanistan to ensure that as atrocities against non-Jewish people in the Middle East continue and continue to generate anger among the people of the Middle East, the people of the Middle East remain, to the degree possible, unable to attack the United States and West in retaliation.