When you login to Facebook, you give it your name, age, gender, and the fact that you used to like 30 Rock. When you click on nonsense, Facebook crawls what you just read and figures out what it says about you. When you browse sites, Facebook's like button sends signals back to Facebook. You login across every device - your computer, phone, etc. Long story short, Facebook has an awful lot of userdata, and it's a champ at cross-device attribution.
The 800-pound gorilla in advertising is, of course, Google. They make their money through search ads and display ads. The display ads program runs broadly through DoubleClick, but the pure publisher monetization product is often called AdSense. AdSense uses contextual data about the publisher's page, AdX (Google's ad exchange), and Google's search data about the user as inputs to monetize each impression. Google makes a significant chunk of its money from AdSense (they make more from search, but still).
Facebook has the Facebook Audience Network (FAN). This uses Facebook's user data and latent demand to monetize on third parties. Until recently, this was only mobile apps. Facebook had decent distribution there, but (to the best of my understanding), their SDK distribution paled significantly behind the likes of MoPub/Twitter, Millenial/AOL/Verizon, Google/AdMob and maybe others - in terms of absolute volume. That said, I understand they did a pretty good job monetizing. As we've discussed, it's no joke getting SDK distribution, but they still did well.