[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]