Let $(X,d)$ be a metric space.
I want to show that $A\subset X$ is closed if and only if every continuous function $f:A\rightarrow \mathbb R$ can be extended to a continuous function $\hat f:X\rightarrow \mathbb R$.
($\implies$) This direction is a direct application of Tietze's extension theorem, which states that $\mathbb R$ is an absolute extensor for the class of normal topological spaces, which also includes all metric spaces.
I've problems with the other direction. What's upon us to prove is that for a non-closed set $A$ in a metric space there always exists a continuous function which cannot be continuously extended to the whole space. It's enough to show that it cannot be extended to $\overline A$; were it possible to do so, we could as well extend it to $X$ by Tietze's theorem.
($\impliedby$) We need to find a function, such that its only possible extension is discontinuous. First, notice that since $\overline A\neq A$, $\exists a\in\overline A\setminus A\subset \operatorname{Fr}{A}$. Let $f:A\rightarrow \mathbb R$ be defined as $f(x)=\frac{1}{d(x, a)}$. It is continuous, since the metric is always continuous and $x\neq a$ for all $x\in A$. What I need to show now (to really be sure that $f$ cannot be continuously extended to $\overline A$) is that the only possible candidate for $\hat f:\overline A\rightarrow \mathbb R$ is $\hat f(x)=\frac{1}{d(x,a)}$, which is discountinuous at $a$.
My attempt:
I notice that since $a\in\operatorname{Fr}{A}, \forall \delta>0: B(a,\varepsilon)\cap A\neq \emptyset$ i.e. $\exists x\in A:d(x,a)<\delta$. Now I try yielding a contradiction; suppose $\hat f:\overline A\rightarrow \mathbb R$ is continuous and $\hat f|_A\equiv f$. Then let $\varepsilon>0$; since $f$ is continuous at $a$, there exists $\delta >0$ such that $d(x,a)<\delta$ implies $|f(x)-f(a)|<\varepsilon$. Here we choose $x\in A$. By triangle inequality of $|.|$ norm, we have $|f(a)|\geq \underbrace{|f(x)-f(a)|}_{<\varepsilon}-\underbrace{\frac{1}{d(x,a)}}_{>1/\delta}$.
It seems like I'm not far from yielding a contradiction, but really can't see it.
The contradiction will come in some form of
In what exact form one presents that contradiction is a matter of taste.
I like the $\lim_{\substack{x\to a\\x\in A}} f(x) = +\infty$ one because it's concise, but we can as well spell it out with explicit $\varepsilon$ and $\delta$. Let $c\in \mathbb{R}$. Then for $\delta < \frac{1}{1 + \lvert c\rvert}$ and $x \in A$ with $d(x,a) < \delta$ we have
$$\lvert f(x) - c\rvert \geqslant \frac{1}{\delta} - \lvert c\rvert > 1\,,$$
so $c$ cannot be the value of a continuous extension of $f$ at $a$.