Let $D\subset\mathbb R^n$ be open and bounded. Consider $\Delta u=f$ in $\Omega$ and $u=g$ on $\partial\Omega$. Let $g\in W^{1,2}(D)$ and $f\in L^\infty(D)$.
Then the minimizer of $$ I(u)=\int_\Omega |\nabla u|^2+2fu dx $$ over $U=\{u\in W^{1,2}(\Omega):u-g\in W_0^{1,2}(\Omega)\}$ solves the poisson equation given above.
My question is why do we have $u-g\in W_0^{1,2}(\Omega)$? Why do we get then $u=g$ on $\partial \Omega$? Are these conditions equivalent or does any implication hold?
The space $W^{1,2}_0(D)$ is appropriately differentiable functions with support in the interior of $D$. It is the natural space to consider when solving $\Delta u = f$ with $u=0$ on the boundary. To solve this problem, you develop machinery to work on $W^{1,2}_0$.
To tackle the general problem, you want to make use of the theory you've already developed. Therefore you translate the general problem into the homogeneous problem by considering functions with $u-g = 0$ on the boundary.
In other words, the only way you know to find the appropriate $u$ in $W^{1,2}$ is by using the existing machine to find $u-g$ in $W^{1,2}_0$.