Suppose K is not compact. Then, by Heine Borel theorem, K is not bounded or not closed.
If K is not bounded, define f(x) = ||x||, x ∈ K. Then, f is continuous. And since K is unbounded, for every M > 0 there is x ∈ K such that f(x) = ||x|| > M, which shows f is a continuous unbounded function from K to R.
If K is not closed, then K has a limit point a that doesn't belong to K. So, the function from K to R given by f(x) = 1/||(x - a|| is well defined and continuous. Since a is a limit point of K, then for every M > 0 we can find x ∈ K such that ||x - a|| < 1/M, which implies that f(x) > M. Hence, f is unbounded.
This shows that, if K is not compact, then there is a continuous f:K → R that's not bounded. By contraposition, the claim is proved.