This is the question Michael Munger asks in his essay Bosses Don't Wear Bunny Slippers: If Markets Are So Great, Why Are There Firms? This is also the question Ronald Coase asked 70 years before. Munger asks
If prices and competition do such a terrific job of directing resources (and they do!), then why are there firms? Why are there hierarchical organizations that are internally directed by command and control, rather than the price system? Why not outsource everything? Why don't bosses sit home wearing bunny slippers?Coase, clearly not a man for bunny slippers, explains his view of the problem this way,
However, in 1931, I had a great stroke of luck. Arnold Plant was appointed professor of commerce in 1930. He was a wonderful teacher. I began to attend his seminar in 1931, some five months before I took the final examinations. It was a revelation. He quoted Sir Arthur Salter: "The normal economic system works itself." And he explained how a competitive economic system co-ordinated by prices would lead to the production of goods and services which consumers valued most highly. [...] Plant had described in his lectures the different ways in which various industries were organised but we seemed to lack any theory which would explain these differences. I set out to find it. There was also another puzzle which, in my mind, needed to be solved and which seemed to be related to my main project. The view of the pricing system as a co-ordinating mechanism was clearly right but there were aspects of the argument which troubled me. [...] Competition, according to Plant, acting through a system of prices, would do all the co-ordination necessary. And yet we had a factor of production, management, whose function was to co-ordinate. Why was it needed if the pricing system provided all the co-ordination necessary? (The Institutional Structure of Production. The 1991 Alfred Nobel Memorial Prize Lecture in Economic Science, delivered 9 December 1991.)Coase goes on to explain that he had
... found the answer by the summer of 1932. It was to realise that there were costs of using the pricing mechanism. What the prices are have to be discovered. There are negotiations to be undertaken, contracts to be drawn up, inspections to be made, arrangements to be made to settle disputes, and so on. These costs have come to be known as transaction costs. Their existence implies that methods of coordinationIt was for his discovery and clarification of the significance of transaction costs and property rights for the functioning of the economy that Coase was awarded the 1991 Nobel Prize in Economics.
alternative to the market, which are themselves costly and in various ways imperfect, may nonetheless be preferable to relying on the pricing mechanism, the only method of co-ordination normally analysed by economists. It was the avoidance of the costs of carrying out transactions through the market that could explain the existence of the firm, in which the allocation of factors came about as a result of administrative decisions (and I thought it did explain it). (The Institutional Structure of Production. The 1991 Alfred Nobel Memorial Prize Lecture in Economic Science, delivered 9 December 1991.)
Munger explains Coase's answer this way,
His remarkable 1937 paper in Economica contained two key insights. First, firms are contractual means of reducing transactions costs. Division of labor requires groups, sometimes large groups, of workers. But it would be too expensive and time-consuming to negotiate sales of labor, services, and products at every stage of production. So, an entity called "the firm" is created, which specializes in directing these activities. Firms compete with each other, but within the firm, activities are directed by command and control.
Second, Coase argues that the optimal size of firms responds directly, though in undirected ways, to market forces. This is true both for vertical integration (owning suppliers, and retail outlets) and market share (the number of units sold, total). So the market is at work after all, since the expansion or contraction of the firm is directed by prices and the actions of consumers and suppliers. Firms that guess wrong, and expand (or contract) too much will lose profits, and may even be "selected" for extinction by bankruptcy.