[proofplan]
We prove that the image $S := f(X)$ is a nonempty compact connected subset of $\mathbb{R}$. Compactness gives [real numbers](/page/Real%20Numbers) $a = \inf S$ and $b = \sup S$, and also ensures that both endpoints belong to $S$. Connectedness then rules out gaps between $a$ and $b$, so every point of the interval $[a,b]$ lies in $S$.
[/proofplan]
[step:Show that the image is nonempty, compact, and connected]
Define the subset $S \subset \mathbb{R}$ by
\begin{align*}
S := f(X).
\end{align*}
Since $X$ is nonempty, choose $x_0 \in X$. Then $f(x_0) \in S$, so $S \neq \varnothing$.
We prove compactness of $S$ directly from the open-cover definition. Let $\mathcal{U}$ be an open cover of $S$ in $\mathbb{R}$. For each $U \in \mathcal{U}$, the preimage $f^{-1}(U)$ is open in $X$ because $f$ is continuous. Since $\mathcal{U}$ covers $S = f(X)$, the family
\begin{align*}
\{f^{-1}(U) : U \in \mathcal{U}\}
\end{align*}
covers $X$. Compactness of $X$ gives finitely many sets $U_1,\dots,U_m \in \mathcal{U}$ such that
\begin{align*}
X = \bigcup_{j=1}^{m} f^{-1}(U_j).
\end{align*}
Applying $f$ to both sides gives
\begin{align*}
S = f(X) \subset \bigcup_{j=1}^{m} U_j.
\end{align*}
Thus every open cover of $S$ has a finite subcover, so $S$ is compact.
We next prove that $S$ is connected. Define the surjective map
\begin{align*}
f_S: X &\to S \\
x &\mapsto f(x).
\end{align*}
The map $f_S$ is continuous because a subset $V \subset S$ is open in the [subspace topology](/page/Subspace%20Topology) exactly when $V = S \cap O$ for some [open set](/page/Open%20Set) $O \subset \mathbb{R}$, and then
\begin{align*}
f_S^{-1}(V) = f^{-1}(O),
\end{align*}
which is open in $X$ by continuity of $f$. Suppose, for contradiction, that $S$ is disconnected. Then there exist nonempty disjoint open subsets $A,B \subset S$ such that
\begin{align*}
S = A \cup B.
\end{align*}
Since $f_S$ is continuous and surjective, the sets $f_S^{-1}(A)$ and $f_S^{-1}(B)$ are nonempty disjoint open subsets of $X$ whose union is $X$. This contradicts the connectedness of $X$. Hence $S$ is connected.
[/step]
[step:Extract the endpoint values from compactness]
Since $S$ is compact and nonempty, it is bounded. Indeed, the open intervals $(-n,n)$ for $n \in \mathbb{N}$ cover $\mathbb{R}$ and hence cover $S$; compactness gives $N \in \mathbb{N}$ such that $S \subset (-N,N)$.
Define
\begin{align*}
a := \inf S, \qquad b := \sup S.
\end{align*}
Then $a,b \in \mathbb{R}$ and $a \leq b$ because $S$ is nonempty and bounded.
We next show that $a,b \in S$. Suppose first that $a \notin S$. Since $a = \inf S$, every $s \in S$ satisfies $s > a$. The family of relatively open sets
\begin{align*}
\{S \cap (a + 1/n,\infty) : n \in \mathbb{N}\}
\end{align*}
covers $S$, because for each $s \in S$ the strict inequality $s-a>0$ allows a choice of $n \in \mathbb{N}$ with $1/n < s-a$. Compactness of $S$ gives finitely many indices $n_1,\dots,n_m \in \mathbb{N}$ such that
\begin{align*}
S \subset \bigcup_{j=1}^{m} \bigl(S \cap (a + 1/n_j,\infty)\bigr).
\end{align*}
Let $N := \max\{n_1,\dots,n_m\}$. Then $S \subset (a+1/N,\infty)$, so $a+1/N$ is a lower bound for $S$ strictly larger than $a$, contradicting $a = \inf S$. Hence $a \in S$. Applying the same argument to the relatively open cover
\begin{align*}
\{S \cap (-\infty,b - 1/n) : n \in \mathbb{N}\}
\end{align*}
shows that $b \in S$.
[guided]
The compactness of $S$ gives two pieces of information: boundedness and endpoint attainment. First, boundedness follows from the open cover
\begin{align*}
\mathbb{R} = \bigcup_{n=1}^{\infty} (-n,n).
\end{align*}
Because $S$ is compact, finitely many of these intervals cover $S$. Taking the largest index among the finite subcover, there is $N \in \mathbb{N}$ such that $S \subset (-N,N)$. Therefore the real numbers
\begin{align*}
a := \inf S, \qquad b := \sup S
\end{align*}
exist.
Now we need the endpoints to belong to $S$, not merely to bound it. We prove this directly from compactness. Suppose first that $a \notin S$. Since $a$ is the greatest lower bound of $S$, every point $s \in S$ satisfies $s > a$. For each such $s$, choose $n \in \mathbb{N}$ with $1/n < s-a$; this is possible by the [Archimedean property](/page/Archimedean%20Property) of $\mathbb{R}$. Hence $s \in S \cap (a+1/n,\infty)$, so the relatively open family
\begin{align*}
\{S \cap (a+1/n,\infty) : n \in \mathbb{N}\}
\end{align*}
covers $S$.
By compactness, finitely many of these sets cover $S$. If the chosen indices are $n_1,\dots,n_m$, define $N := \max\{n_1,\dots,n_m\}$. Then every point of $S$ lies in $(a+1/N,\infty)$, so $a+1/N$ is a lower bound for $S$. This lower bound is strictly larger than $a$, contradicting $a = \inf S$. Therefore $a \in S$.
The supremum endpoint is handled in the symmetric way. If $b \notin S$, then every $s \in S$ satisfies $s < b$, and the relatively open family
\begin{align*}
\{S \cap (-\infty,b-1/n) : n \in \mathbb{N}\}
\end{align*}
covers $S$. Compactness gives a finite subcover, and with $N$ equal to the maximum chosen index we get $S \subset (-\infty,b-1/N)$. Then $b-1/N$ is an upper bound for $S$ strictly smaller than $b$, contradicting $b = \sup S$. Hence $b \in S$.
[/guided]
[/step]
[step:Use connectedness to rule out gaps inside the interval]
We prove that $[a,b] \subset S$. Let $t \in [a,b]$. If $t=a$ or $t=b$, then $t \in S$ by the previous step. Assume $a < t < b$.
Suppose, for contradiction, that $t \notin S$. Define
\begin{align*}
A := S \cap (-\infty,t), \qquad B := S \cap (t,\infty).
\end{align*}
The sets $A$ and $B$ are open in the subspace topology on $S$, disjoint, and satisfy
\begin{align*}
S = A \cup B,
\end{align*}
because $t \notin S$. Since $a \in S$ and $a<t$, we have $a \in A$, so $A \neq \varnothing$. Since $b \in S$ and $t<b$, we have $b \in B$, so $B \neq \varnothing$.
Thus $S$ is separated into two nonempty disjoint open subsets, contradicting connectedness of $S$. Therefore no such $t$ exists, and $[a,b] \subset S$.
[/step]
[step:Conclude that the image is exactly the closed interval]
By the definitions of $a = \inf S$ and $b = \sup S$, every element of $S$ lies between $a$ and $b$, so
\begin{align*}
S \subset [a,b].
\end{align*}
The previous step gives the reverse inclusion $[a,b] \subset S$. Therefore
\begin{align*}
f(X) = S = [a,b].
\end{align*}
This proves the theorem.
[/step]