The Bay Area's land-use policies are directly responsible for the absurdly high housing costs and terrible road congestion. Too many jobs are in office parks, too many houses are in far-flung and low-density areas.
If Facebook can build 1000 apartment homes that would allow their employees to walk to work, they stand to benefit in huge ways. If that development can help subsidize 200 apartment homes for people who have been displaced by the skewed housing market, all the better.
At least that's how I read their motivations.
I imagine Facebook has similar considerations. Housing and traffic are major issues in Silicon Valley.
Plus, the types of companies we're talking about have wads of cash that they're not returning to shareholders (yet). Real estate investments could provide better returns than a a number of other financial instruments. Win-win. :)