Compactness of $(M, d)$ in terms of continuous functions $f:M\to \mathbb R$
Solution 1:
If $M$ is not compact then there is a sequence $(x_n)$ without convergent subsequences. The sequence is a closed discrete subspace so the map $f: \{x_n:n\in\mathbb N\}\to \mathbb R$ given by $x_n\to n$ is continuous. By the Tietze extension theorem it has a continuous extension $F: M\to\mathbb R$. Clearly, $F$ is unbounded.
Solution 2:
This is not meant to replace azarel’s answer, but rather to show in more detail just how unbounded real-valued functions arise in non-compact metric spaces.
Recall that if a metric space $\langle X,d\rangle$ is not compact, then either it’s not complete, or it’s not totally bounded.
If it’s not complete, it has a Cauchy sequence $\langle x_n:n\in\omega\rangle$ that does not converge; define $$f:X\to\mathbb{R}:x\mapsto\lim_{n\to\infty}d(x,x_n)\;.$$
Intuitively, $f(x)$ is the distance from $x$ to the non-existent point to which the Cauchy sequence wants to converge, so you’d expect $f$ to exist and be continuous, and indeed this isn’t hard to prove. Moreover, $f(x)$ is never $0$, because the Cauchy sequence has no limit in $X$. Thus, the function $$g:X\to\mathbb{R}:x\mapsto\frac1{f(x)}$$ is continuous, and it’s also clearly unbounded, since $$\lim_{n\to\infty}g(x_n)=\infty\;.$$ In effect we’ve pushed that non-existent limit of the Cauchy sequence out to be a point at infinity.
If $X$ is not totally bounded, there is an $r>0$ such that no finite collection of open $r$-balls covers $X$. Thus, we may recursively construct a sequence $\langle x_n:n\in\omega\rangle$ such that for each $n\in\omega$, $$x_n\in X\setminus\bigcup_{k<n}B(x_k,r)\;,$$ where $B(x_k,r)$ is the open ball of radius $r$ centred at $x_k$. Note that $d(x_m,x_n)\ge r$ whenever $m<n<\omega$, so $\{B(x_n,r/3):n\in\omega\}$ is an infinite family of pairwise disjoint open balls of radius $r/3$. Now let
$$\begin{align*} g&:X\to\mathbb{R}:\\ &x\mapsto\begin{cases} 0,&\text{if }x\in X\setminus\bigcup_{n\in\omega}B\left(x_n,\frac{r}3\right)\\\\ \frac{3n}r\left(\frac{r}3-d(x,x_n)\right),&\text{if }x\in B\left(x_n,\frac{r}3\right)\;. \end{cases}\end{align*}$$
Clearly $g(x_n)=n$ for each $n\in\omega$, so $g$ is unbounded, and once again it isn’t hard to show that $g$ is continuous.
This time the intuitive idea is to replace the continuous function $$B(x,s)\to\mathbb{R}:y\mapsto d(x,y)$$ that measures distance from the centre of a ball with the function $$B(x,s)\to\mathbb{R}:y\mapsto s-d(x,y)\tag{1}$$ that in a sense measures distance from the boundary of the ball, and then ‘blow up’ the function in $(1)$, making the ball’s radius look bigger than $s$. Since we have infinitely many balls to play with, and they don’t interfere with one another, we can get arbitrarily large ‘blow-up’.