Buyers and sellers interact in markets. Market equilibrium occurs when the desires of buyers and sellers align exactly so that neither group has reason to change its behavior. The market equilibrium price, p*, and equilibrium quantity, q*, are determined by where the demand curve of the buyers, D, crosses the supply curve of the sellers, S. At that price, the amount that the buyers demand equals the amount that the sellers offer.
In the absence of externalities (costs or benefits that fall on persons not directly involved in an activity), the market equilibrium quantity, q*, is also the socially optimal output level. For each unit from 0 up to q*, the demand curve is above the supply curve, meaning that people are willing to pay more to buy those units than they cost to produce. There are gains from producing and then consuming those units.