In a 1970 piece in the New York Times magazine Milton Friedman argued that 'the social responsibility of business is to increase its profits'. They should make as much cash as possible for their shareholders, and shareholders should give directly to charity. It is hard enough to be an efficient firm, without needing to be an effective charity at the same time. But it is popularly believed that corporations' role in society does include various other responsibilities rather than simply maximising long term shareholder value. And in real life we note that firms often run charity events, match their employees' charitable donations and so on. Why would firms spend money on charity when they don't have to?
At first you might expect that managers are exploiting the firm to selfishly gain themselves prestige. There is some evidence, for example, that more narcissistic chief executives do more corporate social responsibility.
But the bulk of evidence suggests that firms do better financially when their 'corporate social performance' is higher. A 2003 meta-analysis of 52 papers and 33,878 firms found a positive association—though this was stronger when you measured financial performance by accounting measures rather than investor measures. A 2007 meta-analysis looked at 167 studies and found a similar result: corporate social responsibility is associated with higher financial performance, though quite weakly.
Various different types of study confirm this point from different angles. For example, a 1997 paper looked only at 27 event studies of share prices when firms revealed socially irresponsible behaviour and found the converse of the other results: bad behaviour cut firm value. Event studies on financial markets are quite a good way of isolating causality; with a short enough timescale the change in question is very likely to be the one driving price changes.
But why exactly does CSR help firm performance? Recent work provides some clues. For one, it seems to cut a firm's financial risk. It seems to raise a firm's access to capital. This might be why market analysts tend to recommend firms more in their notes after they engage in CSR. And it explains why firms with more shareholder-driven corporate governance give more incentives to CEOs to engage in CSR—not what you'd predict if it was an agency cost.
This probably comes from reputational improvements, and reputational insurance. Customers prefer to buy from firms who do more and better social programmes, and engaging in CSR seems to cushion stock declines after ethics in business become popularly salient (e.g. after the 1999 Seattle protests against the WTO).
Intriguingly, the reputational advantages may also extend to the government. Davis et al. (2015) discovers that firms who do more nice stuff also lobby more and pay less tax; i.e. that corporate social responsibility and tax are substitutes. This suggests that CSR overall is not driven simply by some measure of manager altruism or empathy or quality—the sort of thing we might usually wonder about. (Though some evidence disagrees.)
None of this really answers whether CSR is good for society at large. If it does enhance reputation, leading consumers to like it more, then this is basically a transfer from consumers to charities. Either way we probably shouldn't lionise firms when they do it—they're just trying to maximise profits, as usual.