We work by contradiction. We take as our hypothesis that every continuous function on $K$ is bounded. We assume $K$ is not bounded and demonstrate a contradiction. Then we assume that $K$ is not closed and demonstrate another contradiction.
As noted above, if $K$ is unbounded, the identity function on $K$ also is unbounded.
If $K$ is not closed, then $A=\Bbb R \setminus K$ is not open. Choose $x \in A$ such that there is no open ball around $x$ that is contained in $A$. Such an $x$ must exist because $A$ is not open. That means every open ball around $x$, no matter how small, has non-empty intersection with $K$, so you can get as close as you like to $x$ and still stay within $K$.
Then $f: K \to \Bbb R$ defined by $f(y)= \dfrac{1}{y-x}$ is continuous and unbounded on $K$. The function $f$ is continuous on $K$ because it's continuous on $\Bbb R \setminus \{x \}$ and $x \in A \Rightarrow x \notin K$ by the definition of $A$, so $K \subseteq \Bbb R \setminus \{ x \}$. It's unbounded because we just showed that we can make $\vert y - x \vert$ arbitrarily small and still stay within $K$, which means we can make $\vert f(x) \vert =\left \vert \dfrac{1}{y-x} \right \vert$ arbitrarily large within $K$.
In higher dimensions, use the function $\dfrac {1}{d(y, x)}$ to the same effect.
Thus, if all continuous functions on $K$ are bounded, it follows that $K$ must be closed and bounded; in other words, compact.
A set $A$ is called compact if a finite set can be extracted from any of its covers by open sets. Then the theorem was introduced: $K$ is a compact if $K$ is bounded and closed