Put the two halves of the consumer's problem together: what she would like (her The Marginal Rate of Substitution) and what she can afford (the Budget Sets). The best affordable bundle is the point of the budget set on the highest indifference curve. With calculus it is a constrained version of Optimization.
🔑 Key idea
At an interior optimum (some of both goods, smooth curves), $$MRS = \frac{MU_x}{MU_y} = \frac{p_1}{p_2} \quad\text{and}\quad p_1x + p_2y = m.$$ Equivalently, $\dfrac{MU_x}{p_1} = \dfrac{MU_y}{p_2}$: the last dollar spent on each good adds the same utility.
💡 Why it works
The MRS is what one more unit of $x$ is worth to her, in units of $y$. The price ratio is what it costs, in units of $y$. If $MRS > p_1/p_2$, she can give up $p_1/p_2$ units of $y$ to buy one more $x$, which she values at more than that: utility rises, so she should keep moving toward more $x$. If $MRS < p_1/p_2$, the reverse. Only where the two are equal does no small move along the budget line help. In calculus terms: substitute $y = (m - p_1x)/p_2$ into $U$ and differentiate with the chain rule: $$\frac{d}{dx}U\!\left(x, \frac{m - p_1x}{p_2}\right) = MU_x - \frac{p_1}{p_2}MU_y = 0.$$
$U = \sqrt{xy}$ with \$80, $p_1 = 2$, $p_2 = 4$. At (8, 16) the MRS is 2 while the price ratio is 1/2: she values $x$ more than it costs. The best bundle is (20, 10), where the curve just touches the line.
✏️ Worked example: Cobb-Douglas by substitution
$U = xy$, $p_1 = 2$, $p_2 = 4$, $m = 80$. Find the best bundle.
$MRS = \dfrac{2}{\sqrt{x}}$. Tangency: $\dfrac{2}{\sqrt{x}} = \dfrac{1}{2}$, so $\sqrt{x} = 4$ and $x = 16$.
That costs \$16, leaving \$24 for $y$: $y = 12$.
Any extra income goes to $y$; the demand for $x$ depends only on prices (as long as income covers 16 units).
Answer: $(16, 12)$.
⚠️ Common mistake
Setting the MRS equal to $p_2/p_1$, or finding a tangency point that is not on the budget line. The MRS has $MU_x$ on top and the price ratio must have $p_1$ (the price of $x$) on top too: both are "units of $y$ per unit of $x$." And the tangency condition alone is one equation in two unknowns; you always need the budget too. Check: plug your bundle back into the budget (it must cost exactly $m$) and compute the MRS there (it must equal $p_1/p_2$).
The tangency gives the maximum when indifference curves are bowed toward the origin (convex preferences) and the solution has positive amounts of both goods. When the tangency would need a negative amount, or the curves are straight or kinked, the best bundle is a corner or a kink: see Corners and Kinks.
Practice
Pass with 3 right answers, the last 2 in a row. Each right answer earns 3 XP.