More accustomed to teams bouncing all around due to relegation and elevation, huh? That doesn't happen in baseball.
The National League was founded in 1876. For the first 25 seasons, membership varied -- teams started, teams folded, teams moved, sometimes a team would switch leagues (other leagues came and went at times). The original NL had eight teams (two still in operation today -- the Braves and Cubs). By 1900 it had grown to 12, but after that season, it contracted (folded teams) down to eight. So the 1901 National League season had, by today's names (there's been some renamings and moves since), the Braves, Phillies, Pirates, Cubs, Cardinals, Reds, Giants, and Dodgers.
The American League opened its doors in 1901, also with eight teams. The leagues were in competition for a few years, but got together to play the first World Series championship in 1903, and a few years later merged into Major League Baseball. However, despite the umbrella organization, they continued to operate as separate entities.
After fending off another competing league, the Federal League, which operated from 1914-15, the remnants of the FL sued MLB for acting anti-competitively, in violation of the Sherman Anti-Trust Act. The case reached the Supreme Court in 1922, and in a decision that continues to baffle legal scholars and fans to this day, the SC ruled that baseball was not interstate commerce (which it clearly was) and thus not subject to Sherman. This ruling (which has come before the SC a few times since, and every time the Justices state that (a) yes, it was a bad ruling but (b) we're not going to overturn it, get Congress to take care of that if you really want it done) is the basis for MLB's anti-trust exemption, a legal standing for which other businesses would kill (more than they do already) but cannot get (the NFL has tried).
Speed along to 1959. Despite population growth and migration, MLB still reaches no further west or south than St. Louis. Other cities, interested in getting big league baseball, decide to form the Continental League. The threat of competition is real, and MLB strikes a deal instead. The CL folds (without ever playing a game), but MLB agrees to expand by four teams -- two new AL franchises in 1961 (the Angels and a new Senators to replace the one that moved to Minnesota) and two NL members in 1962 (the Mets and Colt .45's, now the Astros). This proves to be sufficiently successful that MLB takes another great leap forward in 1969, adding four teams (the Padres and Expos (now Nationals) in the NL, the Royals and Pilots (now the Brewers) in the AL).
The Pilots leave for Milwaukee and become the Brewers after one season, and a plan to move the Giants to Toronto that collapses, leads to legal threats to which MLB again caves, and expands by two more teams in 1977, adding the Mariners and Blue Jays to the AL. The AL thus had a 14 team to 12 NL team advantage for a while.
Eventually it's time to balance it out again, and the NL adds the Marlins and Rockies in 1993. However, the sitting commissioner, Vincent, divvied up the expansion fees ($190M) heavily in favor of the NL franchises. Understandable, sure, but the AL had to provide equivalent numbers of players for the expansion draft, and that really ticked off the AL owners.
So, when the most recent expansion came around in 1998, one team went into each league so the expansion fees (about $250M this time) could be divided equitably without rancor. But the leagues didn't want to have an odd number of teams each, so one team, Milwaukee, jumped from the AL to the NL to keep the memberships even-numbered. And that's where we are today.
With the exception of the Brewers, the teams are in the leagues they are in because that's the league they've ALWAYS been in. And the Brewers, at the time owned by the sitting acting commissioner, agreed to the change.