In this note, we briefly prove a well-known claim.
Claim: let $X,Y$ be real-valued random variables. Let $f$ be a differentiable function. Then $\mathbb{E}[Y|X]$ is the (almost sure) unique minimizer of
$$ \min_f\mathbb{E}\left[ (Y-f(X))^2 \right] $$
We expand
$$ \mathbb{E}\left[ (Y-f(X))^2 \right] = \underbrace{\mathbb{E}\left[ (Y-\mathbb{E}[Y|X])^2 \right]}_{\text{indep. of $f$}} + \mathbb{E}\left[ (f(X)-\mathbb{E}[Y|X])^2 \right] $$
The minimizer over all $f$ for $\mathbb{E}\left[ Y-f(X))^2\right]$ is equivalent to that of $\mathbb{E}\left[ (f(X)-\mathbb{E}[Y|X])^2\right]$. The second term is almost surely nonnegative, and becomes zero at $f(X) = \mathbb{E}[Y|X]$.
This choice is unique. Suppose there is $h(X)$ that also minimizes $\mathbb{E}\left[ (f(X)-\mathbb{E}[Y|X])^2\right]$. Then $\int_\Omega (h(x) - \mathbb{E}[Y|X=x])^2dx = 0$, which implies $h(x) = \mathbb{E}[Y|X=x]$ almost surely.