Mostly by offering value to their customers in the form of added services.
Google do it to allow themselves to provide a free email service.
Without the scanning of email done by google, everyone would have to pay for their email services.
You may or may not like the trade off, but apparently MANY people would prefer to have a free email service that provides advertising over a paid for email service that does not.