There is a certain gambling aspect to fantasy baseball, but it's not considered illegal gambling to the U.S. government. In fact, fantasy sports are declared a business in the Unlawful Internet Gambling Enforcement Act of 2006, and therefore is not illegal.
In most leagues, there's a fee to enter the league that covers most of the costs of the league (if you use a stat service) and then covers the winnings. Typically there's also a small fee for transactions in order to discourage players from changing their roster every week, and to raise a little more money for the payout at the end of the season. Many leagues also have a payout for the best team of each week as well for the winner or winners at the end of the season.

