How Do Free Games Make Money?

From Facebook games where you're a virtual farmer to online card games and role-playing games, one trend has taken over within the last few years: companies giving the games away for free. But how does a video game company afford the artists, designers and coders to make these games if they're just giving them away? Is it really a scam, or is there some secret logic to it?