The war in Afghanistan is going on for two reasons.
First, the war is going on becaus there are various groups in Afghanistan that each want power. They are competing with one another to get power in the country.
Second, the war is going on because of the 9/11 terrorist attacks in the United States. The attacks were conducted by Al Qaeda, which had been permitted to train in Afghanistan by the Taliban government which then ruled the country.
After the attacks, the United States attacked in order to overthrow the Taliban. The US and NATO are still involved in Afghanistan because they are trying to prop up the Karzai government in its conflict with the Taliban.
The answer above is strong. There has also been a desire, among some foreign policy experts, to encourage democracies in that region of the world. The hope has been that if more and more nations have democracies, the less likelihood there will be of war in the future. Some people hope that an Iran flanked by democracies might itself become more democratic.
The oversimple answer is that the American reaction to 9/11 was to chase Al-Quaida back into their mountain cave hideaways. But Afghanistan did not exactly harbor them -- one faction of their tribal political system, the Taliban, chose to take their religious beliefs as an excuse for anti-Western political sentiment; they upped the stakes by calling it a "jihad"--a holy war--and automatically involving all Muslim nations, especially Iran (we had already invaded Iraq). They had been given weapons by the Soviets during their failed invasion of Afghanistan, and saw this as the right time to defeat the current corrupt government. Add to this mix the poppy trade, to the Afghans an economic mainstay, and the U.S. so-called "war on drugs," and we have the present mess.
War is and always has been about the acquistion of power. In Afghanistan war is being waged on two fronts. You have the different factions fighting for political control. You also have US and other forces fighting for control and domination of the Al Qaeda forces who are waging wars of terror in Afghanistan and other countries.
Afghanistan is a country that has very few periods in history that it has not been at war, civil war or other. Currently the war is based on the fact that the United States is certain that the people responsible for the 9/11 terrorist attacks were trained in Afghanistan and are currently hiding out in the mountainous areas of the country. If the US was not there it is likely there would still be war going on based on the facts presented in the post above.