That's a serious oversimplification of MLB teams' business models. Yes, ticket sales, marketing deals, food, beer and merchandise do make some money. However, the bulk of the money made by the teams (MLB is, as others have mentioned, a trade association which has been given special exemptions from anti-trust law) is from media deals with the major networks and "Regional Sports Networks" (RSN) which handle all the ads and broadcast details.
A few of the teams have their own RSNs, but most rely on third parties for that. In fact, one of the biggest (with rights to broadcast 14 of the 30 MLB teams) just went bankrupt.
For more information about that, and some really good "inside baseball" (see what I did there?) read this article[0] from The Athletic, published just yesterday.
[0] https://theathletic.com/4409306/2023/04/14/mlb-streaming-tv-...
The article makes a good point about misalignment here. Teams want to win (winning helps sell merch), but the league wants to sell ads (ads need worthwhile content). If these incentives were better aligned, maybe we'd end up with a less boring game that doesn't end up as background noise.