I've just learned from this article that the US has soldiers and bases in the UK and Germany. This is somehow the violation of the sovereignty of those countries. The US attacked Iraq, so it's not surprising that the US has soldiers and bases there, but why the UK and Germany, How this came to be?
There is no surprise here. The US soldiers in the UK and Germany are here at the invite of the respective government, as part of the NATO mutual defence alliance.
They are in these countries, initially, to aid in the defence against the Soviet Union. In the case of Germany, the bases do date back to the 1940s, when Germany was occupied (you might recall that the US, Britain, and the USSR did attack Germany too, in the second world war). But after the establishment of the Federal Republic of Germany, the new Republic maintained bases as it was not strong enough alone to prevent invasion by the Soviet Union. The bases in the UK are airbases, all are actually shared with the RAF. Again, they were set up to give a rapid response to any invasion by the USSR. There are no bases that a UK military commander does not have the right to enter.
With the collapse of the USSR, the bases have been reduced, but it is still nice to have your friends on hand in case there is an emergency that requires a strong military response.
There are British troops stationed in the USA too. The relative size of the countries means that setting up a whole "base" is not needed. There are British troops stationed at Edwards Air Base in California, and there is a full "base" in Germany where the 23 Amphibious Engineer Squadron is based (it is also something of a relic from the 1940s)
Inviting soldiers to stay in your country to share in defence is something that friendly countries do. It is one of the ways of staying friends, cementing the alliance and deepening the relationship. The presence of UK troops in California doesn't mean that Britain is occupying the USA. And the US air bases in the UK doesn't make Britain any less independent. In no way is this analogous to an "occupation". The troops would have to leave if the UK government told them to.
Some people object to the airbases. You might look at the history of Greenham common, an RAF airbase that held US nuclear weapons for an example of some people who objected. For the most part there are no objections, due to the friendly and invitational nature of the stations.
The real reason that we have troops in foreign countries is, unfortunately, that they also serve as de facto sacrificial lambs (although I do not take solace in reminding this to the brave soldiers and their families so stationed).
In essence, an invasion or any act of aggression, including a weapons launch, would be at the peril of killing Americans. This could further precipitate action by the United States Congress to declare war, irrespective of any alliance.
So one darker answer is actually not always for the direct protection of the inviting host country, but for deterrence, as even unintentional killing of Americans, is really never a good option from a political perspective,
Why US troop are in Germany is a long and obvious story: they went in WWII and stayed because of the Cold War. (According to the very al-Jazeera article the OP linked to, the US troops levels in Germany were reduced after 1990, i.e. the end of the Cold War.)
Germans were still somewhat split on the matter of US troop levels, at least in a 2020 survey. And (in the same survey) some 25% of Germans would like all US troops to leave.
Nearly half of people in Germany are in favor of a US plan to withdraw nearly 12,000 of its troops from the European country, according to a representative survey by the research institute YouGov shared Tuesday.
Some 47% of survey respondents said they supported reducing the number of US soldiers in Germany. One in four was in favor of all US soldiers leaving.
Just 28% thought the number of US troops should remain the same and only 4% were in favor of increasing their numbers.
Another 21% declined to answer.
The YouGov online survey took place between July 31 and August 3, 2020 and was commissioned by German news agency dpa, who published the results.
As for the legal angle, a status of forces agreement, i.e. foreign troops staying at the invitation of the host country/government is different from an occupation. Arguably when a country is invaded and its government changed by dubiously democratic means, e.g. likely fraudulent elections, as it happened in many countries that fell into the Soviet sphere during/after WWII, the distinction can be somewhat murky, but even there a number of leaders of said countries managed to obtain the departure of Soviet forces... albeit the Soviets went back in (e.g. in Czechoslovakia) when things diverged too far politically from what the USSR could stomach, which is sometimes called the Brezhnev doctrine, although in practice it existed before.
As for US troops in Iraq, their number was actually zero after the US and Iraq failed to agree (to the details) of such an arrangement, at the end of 2011. And then ISIS happened, which the Iraqi army had trouble containing, so the Americans were invited back in, including requests for attacking ISIS directly. I cannot find a recent poll on what Iraqis might want in terms of US troop levels, but a good number of Iraqi leaders, even some fairly aligned with Iran, presently don't really want a complete departure of Western troops. On the other hand, there definitely is a faction in the Iraqi parliament that did ask just for that, and eventually managed to pass non-binding resolution in early 2020 (after the US killed Iranian general Soleimani, on Iraqi soil). Subsequently, in a move that somewhat resembles what happened in Afghanistan in 2015, the US agreed to change the role of their forces in Iraq to a purely training one by the end of 2021... although some say this will not make a huge practical difference.
The US bases in Iraq are still subject to sporadic but regular attacks from insurgents, something that definitely doesn't happen in Germany, let alone in the UK. Even during the Cold War, when groups like the Red Army Faction operated in West Germany, sometimes targeting US personnel, the security situation didn't compare to Iraq nowadays. So the US forces can definitely have a much more "relaxed" presence in Western Europe, which generally means more personnel than the "tip of the spear". In the Middle East too, the US has or had a more sizeable base presence in (the tiny) Qatar, which it did not invade, including a very large large "prepositioning" (i.e. reinforcement/logistics) base at As Sayliyah and the largest air force base (by area) in the region at Al Udeid. Generally, more advanced US aircraft are/were based there, like F-22s and strategic bombers. I'm not sure of the current troop levels (could not find data), but in 2017 there were 11,000 US troops in Qatar, which substantially exceed what the US sent to Iraq proper against ISIS (although vastly less than what the US had sent to Iraq in the previous decade).
So the present US troop levels in Iraq aren't really a good yardstick for much else... not for what invasion-level troop levels looked like in the same country, and not for what "safe rear bases" in [much] more friendly countries are like.