To maximize profits a firm need not sell the output at the highest price. As a matter of fact in most cases trying to increase the price will result in a loss in revenue and may ultimately lead to a decrease in the profits made. Selling at the highest price may be possible only in the case of a monopoly or where the output is an essential product and has very little price elasticity of demand.
We usually consider a market with perfect competition for the analysis of the optimum price to be charged and the optimum quantity to be produced. In perfect competition the price at which the output has to be sold is its marginal price.
As there is an unlimited demand for the output, the producer can manufacture as much as it likes but it can sell it only at the marginal price. The higher the production, the higher are the profits made.