Optimal Choices Characterized by a Tangency Condition
In some cases, a consumer’s optimal bundle will be characterized by the tangency condition \(MRS = {p_1 \over p_2}\) Let’s think about what this means intiuitively, mathematically, and visually.

Intuitively, when this is the case, the “bang for the buck” from the last unit of each good is the same: that is, the last dollar spent on good 1 generated exactly as much utility as the last dollar spent on good 2. Put another way, a solution characterized by the condition $MRS = p_1/p_2$ balances the costs and benefits of each good.

Visually, optimal solutions like this are characterized by a tangency between the indifference curve passing through the optimal bundle and the budget line. This tangency means that the set of bundles which is preferred to the optimal bundle does not overlap the budget set: that is, if $X^\star$ is the optimal bundle, then if some bundle is strictly preferred to $X^\star$, it must not be affordable.

Mathematically, the optimal bundle will be characterized by two equations:\(\begin{aligned} \text{Tangency condition: }\ \ \ & MRS(x_1,x_2) = \frac{p_1}{p_2}\\ \text{Budget constraint: }\ \ \ & p_1x_1 + p_2x_2 = m \end{aligned}\)Since the optimal bundle occurs when both of these conditions hold, we can think of the optimal bundle as being “the point along the budget constraint at which the MRS is equal to the price ratio.”
It can sometimes be helpful to plot the tangency condition equation; in this case the optimum occurs at the intersection of the tangency condition curve and the budget line:
Notice which of the factors affect which of the conditions:

the consumer’s preferences only affect the tangency condition: when preferences change, the tangency condition pivots toward the good the consumer likes more

the consumer’s income only affects the budget line: when her income changes, it shifts the budget line out and in, but since the price ratio is unaffected, the tangency condition is unchanged

prices affect both the budget constraint and the tangency condition. An increase in the price of good 1, for example, will cause the budget constraint to pivot in; but it will also affect the price ratio and therefore shift the tangency condition as well
When does the tangency condition find the optimal bundle?
The optimal bundle is not always characterized by a tangency condition.
When the budget constraint is a simple straight line, we can establish some conditions that guarantee an optimum at a tangency condition along the budget line:
 The consumer’s preferences are strictly monotonic. This guarantees that the solution will be on the budget line; otherwise, it might be at an interior point.
 The MRS is infinite whenever $x_1 = 0$. This guarantees that the MRS is greater than the price ratio at the vertical intercept of the budget line; so there won’t be a corner solution in which the consumer only buys good 2.
 The MRS is zero whenever $x_2 = 0$. This guarantees that the MRS is less than the price ratio at the horizontal intercept of the budget line; so there won’t be a corner solution in which the consumer only buys good 1.
 The consumer’s preferences are strictly convex, and the MRS is continuous in both $x_1$ and $x_2$. This means the MRS is smoothly decreasing as you move from the upperleft to the bottomright end of the budget constraint.
Putting these all together means that the solution lies along the budget line; that the MRS is greater than the price ratio at the the vertical intercept, smoothly decreases along the budget line, and is less than the price ratio at the horizontal intercept. Therefore, by a continuity argument, there must be a single point at which the MRS equals the price ratio. Perhaps more intuitively and succinctly: monotonicity pulls the consumer up and to the right, convexity pulls them toward an interior solution, and conditions on the endpoints of the budget line ensure that the solution lies in the first quadrant.
When one or more of the above conditions fail, the solution may not be characterized by a tangency condition:

If the consumer’s preferences are not strictly monotonic, their optimal point might involve not spending all their money on these two goods.

If the consumer’s MRS is greater than the price ratio when $x_2 = 0$, their optimal point will be a corner solution in which they spend all their money on good 1. (Likewise, if their MRS is less than the price ratio when $x_1 = 0$, they’ll spend all their money on good 2.)

If the consumer’s MRS is not continuous, their optimal solution might occur at a point where the MRS is not defined. An example we’ve seen with this before is perfect complements.

Finally, if the budget constraint itself has a kink, the consumer’s optimal point might occur at that kink, at which point the price ratio is undefined.
The Lagrange Method of Finding an Optimal Choice
In situations where the optimum is characterized by a tangency condition, the method of Lagrange multipliers from multivariate calculus may be used to find the optimal choice. This is a large enough topic that it deserves its own page.