The bases came about due to the end of the 2nd world war, and more bases were built during the Cold War. After Nazi Germany was defeated, the US government was concerned that 1) Nazis might rise up again, and 2) That Russia would take over all of Germany and become that much more strategically powerful. Also, the West German government was in NO shape to defend itself at the time and was pretty eager to ask for American help to prevent all of Germany falling to the Russians. With Japan, the US government was concerned that the old pro-war, pro-imperial government might survive and return if they left, and they had just lost thousands of lives defeating that government, so they wanted to keep an eye on things. Then the Korean War happened (who caused it is still a matter of contention, but the North, in the end, invaded the South and the UN forces (most of whom were US forces) pushed the North back and almost defeated the North until China pushed the US back to the original borders. Since the Korean War has never official ended, and since the South was devastated and China had a LOT of troops in North Korea at the time, the US built bases to help the South Koreans defend themselves. In reality, the South doesn't need US help anymore - the north is laughably weak in terms of its conventional forces (although it has a couple of low-yield fission-based nukes now).
The other bases around the world are either the result of war or allowed by the government. The Saudi Government, in particular, is very happy that US bases are there, because they hate, hate, hate, hate, HATE the Iranians and they hated Saddam when he was still kicking. The bases in other arab nations are also there for that reason (You won't believe the venom that Persians and Arabs have for each other. Saudi Arabia has openly declared that if Iran gets Nukes, it will build them too - yep, they hate each other so much they are willing to nuke each other if they go to war). The Bases in Iraq and Afghanistan are there because the US is fighting there. The bases in some central asia countries are there because the US paid money to those governments to allow air bases for transport and logistics.