What is the need for the US to have so many military bases abroad? Why do these countries allow it?

The US military is that house guest of dubious benefit, questionable timing, faulty manners, but impeccable credit.

All your base are belong to us!
Every base supports either a specific security goal or overall regional stability. Some were acquired after conflict, such as the ones in Germany, Italy, and Japan. Some were gained in support of allies in conflict, such as the ones in England and Korea. Most are simply there to reduce that tyranny of distance. But all have been considered important enough to spend significant treasure in securing, building, manning, and supporting. These bases also mostly support the host country's goals, whether security, financial, relationship, or a combination. With rare exceptions, the bases are there under agreement with the host country, with many of the host countries specifically asking for more US involvement. This is usually done behind closed doors as it doesn't read well in the local papers.

You've lost that lovin' feeling
Each relationship that has led to foreign basing is different, many changing in character from one decade to the next. As an example, in the aftermath of the Spanish–American War, US leaders felt that the newly-freed Filipinos were incapable of self-rule, leaving their nation open to subjugation by another major power. This led to the US's shameful management until just prior to WWII. The post-war basing agreements were considered strategically vital to counter possible Soviet aggression in the Pacific and the Philippine government agreed. Shortly after the fall of the Soviet Union, the Philippine government felt the crisis was over and asked the US to leave, which they did. After the explosion of Mount Pinatubo, they asked the US to renew the agreement. The US declined.

For years, Japan's sensitivity to nuclear power kept the Nimitz class carriers from being based there. When the last conventional carriers were slated for decommissioning, an agreement was made to shift a nuclear-powered carrier over. The fact that the US made an extensive study to move the ships to Guam undoubtedly also had something to do with Japan's change of heart. Along with a big check.

Clean up on aisle four!
Most of the main bases can be traced to Post-WWII stabilization efforts. After their short involvement in WWI, the US withdrew again to domestic and hemispheric concerns, leaving Eurasia to its own affairs—disastrously as it turned out (though US involvement may have had little effect). After WWII, US leaders saw a return to semi-isolationism as a mistake and instead used the influence gained in the war and the execution of the Marshall Plan as springboards to leave troops overseas. Most of the deployments supported bottling up the spread of communism. Many troops were placed in temporary bases, using host facilities for short term missions, either in direct support of combat units, or as logistics bases. Recent examples include Bagram Airfield and the former Transit Center at Manas.

A list of bases and their supported missions, with host nation feelings on the matter, would be exhausting to read, much less compile. Suffice to say, the US military is in a lot of places. It may just be a couple people or a whole combined combat and command unit. It's a lot.

Love on the rocks
One obvious exception, on many levels, is the US base on Cuba, Guantanamo Bay Naval Base. Just as Cuba was a knife in the US's side for most of the last 50+ years, so "Gitmo" has been poking the Cuban government for much longer. Why a naval base so close to US shores, one that the host government does not support, nor gain any material advantage from? The answer is obvious, but is becoming more interesting as relations are finally starting to thaw. Of course, I'm still waiting to see how the whole Gibraltar-Spain thing is going to play out.

Why don't you put your money where your base is?
Of note, many businesses near the larger bases see significant economic gains just from the US members based there. When a large base in Germany was recently (and temporarily) placed on a base closure list, local residents protested and implored Berlin to insist on it staying. This is not universal, and many bases actually have seen significant local protests. This is especially common after service members commit some crime against the local citizens. These disgraceful acts have strategic ripples in many cases, resulting in changes to basing agreements.

The US has operated out of so many locations, it's hard to nail them all down. From Eaton's Tripoli expedition in 1804 to modern drone operations, temporary positions of opportunity have drawn the US to make arrangements with foreign powers. These bases will pop up and disappear with very few Americans ever noticing, such as the Pakistani bases mentioned by Balaji Viswanathan.

Beat cop
As the self-appointed world police, having bases all over the place does a couple things. First, if the base location doesn't deter hostility, it reduces response time, which can often reduce the required response. Second, it reduces the need for the region's allies to build up as much of a military, which fosters more stability in the region. As the economic burden continues to rise, the US is reducing the size of many bases. This has sparked higher spending on defense from the countries in these areas. It hasn't led to violence, but diverting money to military spending reduces what a nation is going to spend on many other programs. This alone can be destabilizing in many countries.

Where's Waldo?
Opened to its full extent, this is an interesting map. It doesn't detail numbers in each country, and many of the countries have only a few personnel, but there's an obvious overtone to where US interests seem to lie. Of course, military presence doesn't always equate to level of interest. For instance, there are only a couple hundred US military personnel in Israel, few of whom are front-line troops. And as hot as the Taiwan issue is, there is little military interaction with the island's government. No real plans, no coordination. Also note that this map may contain inaccuracies, as "troop presence" may simply indicate that the country participates in joint US exercises and doesn't actually have US troops on its soil (outside of the embassies' Marine contingent).

Have fun stormin' the castle!
The real crux of the problem is that any time the US feels its interests are threatened or gets gooey-eyed at relief worker photos, it throws forward some muscle. In order to respond properly, some folks and equipment are moved in to either do the work or support it. This sucks up unbudgeted money, driving the US deeper in debt, which makes less money available to dedicate to really helping a region. These actions usually increase the instability, ratcheting the cycle of violence. What we need is for everyone to chill the hell out for about a decade. Let's get our ducks in a row, then start killing each other again. I think we could all use the break.

Source: QUORA