Let $(X,d)$ be compact and $f:X \rightarrow \mathbb{R}$ be continuous. Then if $X$ is compact, $f(X)$ is also compact. Compact subsets of $\mathbb{R}$ are closed and bounded. By the completeness axiom $f(X)$ must have a lowest upper bound and greatest lower bound. Thus, $f$ attains maximum and minimum values.
We now prove the opposite direction by contrapositive. Let $X = \mathbb{R}$. Then $X$ is not compact. The function $f(x) = x$ has no upper bound, and thus attains no maximum value. Therefore there exists a continuous function $f : X \rightarrow \mathbb{R}$ that does not attain maximum and minimum values.