In order to maximize profit, companies increase their output of product just until marginal revenue (the income earned by selling one additional unit of a good) equals marginal cost (the cost of producing one additional unit of said good), and no further.
There are three possible situations with respect to marginal revenue and marginal cost: marginal cost is greater than marginal revenue, marginal revenue and marginal cost are equal, and marginal revenue is greater than marginal cost. If marginal cost is greater than marginal revenue, each additional unit of product produced and sold costs the company money, so the company will not increase its output. In fact, it will be forced to reduce its output until additional units no longer represent a net loss in revenue. If marginal cost is equal to marginal revenue, the company has no reason to alter the quantity of production.
However, if marginal revenue is greater than marginal cost, each additional unit sold creates profit for the company. Therefore, to maximize profit, the company will produce more output until each additional unit no longer brings in net profit. At that point, marginal revenue equals marginal cost and additional output ceases. When marginal revenue equals marginal cost, the price of a unit of the product equals the average total cost of all units sold.
For example, if the marginal revenue earned by a candy-making firm for selling one candy bar is $1, then the firm will want to produce exactly as many candy bars as it needs to produce to ensure that its marginal costs are also equal to $1. If that number of candy bars is 500, then the firm would want to produce and sell 500 bars. (Another way to say this is that profit is maximized where the marginal cost curve intersects the marginal revenue curve.)