Variational problem equivalent to minimizing the energy functional Define the boundary value problem for Poisson's equation as:
$$
\left\{\begin{array}{ll}
-\Delta u=f & \text { in } \Omega \\
u=g & \text { on } \Gamma
\end{array},\right.
$$
for given $f: \Omega \rightarrow \mathbb{R}, g: \Gamma \rightarrow \mathbb{R}$ and $\Gamma=\partial \Omega$.
Now we define Dirichlet's energy as:
$$
I: \mathrm{C}^{1}(\bar{\Omega}) \rightarrow \mathbb{R}, \quad I(v):=\frac{1}{2} \int_{\Omega}|\nabla v|^{2} d x-\int_{\Omega} f v d x \quad v \in \mathrm{C}^{1}(\bar{\Omega}),
$$
our class of admissible functions is this one
$$
\mathcal{K}:=\left\{v \in \mathrm{C}^{2}(\bar{\Omega}): v=g \text { on } \Gamma\right\}
$$
Then I know this theorem holds:

Let $\Omega \subset \mathbb{R}^{n}$ be a bounded open set with
$\Gamma=\partial \Omega$ of class $\mathrm{C}^{1}$ and let $f \in
 \mathrm{C}^{0}(\bar{\Omega})$ and $g \in \mathrm{C}^{0}(\Gamma)$.
Assume $u \in \mathcal{K}$ solves the boundary value problem above.
Then $$ I(u)=\min _{v \in \mathcal{K}} I(v) . $$ Viceversa, if $u \in
 \mathcal{K}$ satisfies this minimization problem, then $u$ solves the
boundary-value problem.

But now I found this counterexample by Hadamard:

Let $\mathbb{D}=\left\{x \in \mathbb{R}^{2}:|x|<1\right\}$, and let
$u: \mathbb{D} \rightarrow \mathbb{R}$ be given in polar coordinates
by $$ u(r, \theta)=\sum_{n=1}^{\infty} n^{-2} r^{n !} \sin (n !
 \theta) $$ It is easy to check that each term of the series is
harmonic, and the series converges absolutely uniformly in
$\overline{\mathbb{D}}$. Hence $u$ is harmonic in $\mathbb{D}$ and
continuous in $\overline{\mathbb{D}}$. On the other hand, we have $$
 E(u)=\int_{\mathbb{D}}|\nabla u|^{2} \geq \int_{0}^{2 \pi}
 \int_{0}^{\rho}\left|\partial_{r} u(r, \theta)\right|^{2} r
 \mathrm{~d} r \mathrm{~d} \theta=\sum_{n=1}^{\infty} \frac{\pi n !}{2
 n^{4}} \rho^{2 n !} \geq \sum_{n=1}^{m} \frac{\pi n !}{2 n^{4}}
 \rho^{2 n !}, $$ for any $\rho<1$ and any integer $m$. This implies
that $E(u)=\infty$. To conclude, there exists a Dirichlet datum $g \in
 C(\partial \mathbb{D})$ for which the Dirichlet problem is perfectly
solvable, but the solution cannot be obtained by minimizing the
Dirichlet energy. There is no full equivalence between the Dirichlet
problem and the minimization problem.

How is the theorem ruling out the counterexample?
 A: The theorem is "almost" true, but stated as it is stated it is missing a technical condition (which is actually the one the counterexample is exploiting).
Let me state your theorem correctly:

Let $\Omega \subset \mathbb{R}^{n}$ be a bounded open set with
$\Gamma=\partial \Omega$ of class $\mathrm{C}^{1}$ and let $f \in
 \mathrm{C}^{0}(\bar{\Omega})$ and $g \in \mathrm{C}^{0}(\Gamma)$.
Assume $u \in \mathcal{K}$ solves the boundary value problem

and assume also there exists at least one function in $\mathcal{K}$ making the Dirichlet's integral finite.

Then $$ I(u)=\min _{v \in \mathcal{K}} I(v) . $$ Viceversa, if $u \in
 \mathcal{K}$ satisfies this minimization problem, then $u$ solves the
boundary-value problem.

Indeed notice that in your counterexample, all functions in $\mathcal{K}$, that is all functions that satisfy your "Dirichlet datum" $g \in \mathcal{C}(\partial \mathbb{D}$) have infinite Dirichlet energy by the computation above when you let $\rho = 1$ (that is, on the boundary of your domain).
The moral is the following: this is a nice example that technical conditions are not important as far as "main ideas" are concerned, but are important otherwise you may construct counterexamples that (in general) falsify your theorem.
