Externalities and total surplus in the market
In order for the free market equilibrium to be efficient, markets have to be perfectly competitive AND there cannot be external benefits or costs imposed by the production or consumption of this product on others.
Market equilibrium vs. efficient production
In the graph below, you can add external benefits or costs and see how the society-level marginal cost and marginal benefit curves shift. You can also display both private and external surpluses at any level of production.