Hey, so, I don't think we've ever talked about the theory of the firm before. It's important to think about this if you want to understand what's going wrong right now.
Firms - that is, companies with offices sand employees, multinational corporations, and such - are just groups of workers, which seek to maximize their profit under edict from some appointed internal authority. Markets, on the other hand, are unaffiliated agents which seek to maximize their profit by competing against each other. It might be hard to get your head around this, but firms and markets really have the same job; there's nothing a firm can do that a market can't, and vice-versa. A common example of this is subcontracting. Something that happens a lot is a manager/business owner using contract labor to develop a product, and then outsourcing the manufacturing to some established company (usually in China). In this case, the product was never built by a "firm"; it's markets all the way down. Any business can operate this way, no matter the size: a CEO can subcontract managers to subcontract other managers, who subcontract engineers for piecework, and so-forth. It's easier to understand the converse (a monopoly). The point is, you never actually need a firm to get stuff done. Whether you have one firm make all of something, or a bunch of firms, or zero firms, it's a voluntary choice.
It's fairly uncontroversial that competitive markets produce better results in the long run than executive decree, so I won't try very hard to justify it here. Markets have internal competition that push their participants to become more efficient in the long run. Firms don't; they have performance targets (e.g. production quotas) set by management (e.g. Stalin), and if you don't meet those quotas you might be fired (e.g. sent to gulag). It's this market efficiency vs. planning inefficiency tension that underlies the theory of the firm.
The economic question basically goes like this: why firms at all? If markets have better outcomes, what possible advantage is there to having firms?
I believe the simplest and most durable argument concerns transaction costs. If an employer has to go to the market for everything, solicit bids, evaluate bids, select the winner, the process would take a lot of time and cost them a lot of money. It would be more cost effective to simply hire one good person on a permanent basis, rather than hiring temporary contractors for each job. Similarly, if each employee had to compete for every job, they would need to spend a lot of time preparing bids and networking to gain access to future opportunities, in exchange for uncertain work and compensation. This is all a hand-wavy way of saying that firms are a more popular arrangement because employees are cheaper than the superior market outcome would be worth.
But this explanation only goes so far. The argument makes good sense for line jobs, where similar work is on-going, and employers can extract meaningful benefit from the accumulation of human capital in their employees. What about staff jobs, which tend to be more advisory in nature? What about vertical integration, which is very popular among firms, but robs them wholly of the right to solicit competitive bids from downstream suppliers?
Consider, for example, your average corporate IT department. Almost anybody who has worked for a large corporation would agree that they are not well-served by their IT department; tail-wagging-the-dog, explicit policies put in place to make their administrative efforts more efficient at the expense of the line workers who feed them. They behave that way because they have no competition; there is no other IT department threatening to take their IT contract away, so they can do basically anything they want short of gross malfeasance. Almost any company would be better served outsourcing IT services under a firm SLA that emphasizes the needs of line workers, rather than dealing with the inevitable inefficiencies and corruption of an internal IT department. The same argument applies to all other staff departments and positions, from benefits coordination to legal and all the way into senior management. Effectively, vertically integrated firms are paying themselves monopoly rents. Or, rather, they are losing it to various inefficiencies. IIRC, S&P 500 companies now have something like 6-8 staff positions for every line position. That means every worker who contributes to the bottom line has to generate enough revenue to pay for 7 other workers who don't.
It seems obvious to me that there's more to this bloat and incompetence than can be explained through transaction cost aversion alone. And, sure enough, there's been a lot of good economics work in this area. I don't fully understand even the most popular arguments, so I won't try to explain them here. You can look them up yourself. The podcast Eversor linked raised an interesting idea, that firms are stuck in a horizontal/vertical integration arms race against each other, and that's why they keep getting bigger even if it's not otherwise profitable for them to do so. Anyway, I'm only mentioning this stuff to emphasize the fact that, not only is firms vs. markets a choice, firms aren't even an obviously good one.
So again, why firms? Should we have them at all? When we're talking about Amazon becoming the world's first trillion-dollar company, whether Google should be broken up, or stuff like that, these are the kinds of questions we're really asking. If Facebook bought Slack, would it actually make all of their employees more productive, or would it just make both companies' owners richer? These are not the same thing.