For a lot of the early years, it lost a lot of money. Providing the bandwidth, getting distribution closer to the ISPs etc was a major investment. Lots of dark fiber.
A bit like Google Maps though, a great visionary early investment that they then poured a lot of $ into to make them what they are today. No one else was just providing free satellite imagery for the entire world back then, not even Google Maps.
The investments to support these two products at least, have been really important in helping Google maintain its hold in other places too.
Lots of people still whinge about youtube, but standing up a solid competitor would take too many $ for anyone but other big tech now.