Write Td for the collection of subsets of X that are open in (X,d), a topology by Metric Open Sets Form a Topology, and TK={K∩W:W∈Td} for the subspace topology on K, which is a topology by The Subspace Topology is a Topology. By the definition of a compact subset, the hypothesis on K says that the topological space (K,TK) is compact. For a natural number n, [n] is the initial segment determined by n. We use the ordered field structure of R, whose order ≤ is in particular a total order, hence reflexive, antisymmetric, transitive and comparing any two elements, and we write s<t to mean s≤t and s=t. We also use the properties of the absolute value recorded in Properties of the Absolute Value in an Ordered Field, in particular t≤∣t∣ and ∣−t∣=∣t∣ for every real t.
Step 1: for every real c the set Vc={y∈K:f(y)<c} belongs to TK.
For each x∈Vc we have f(x)<c, hence 0<c−f(x), so the continuity hypothesis provides a real δx>0 such that every y∈K with d(x,y)<δx satisfies ∣f(y)−f(x)∣<c−f(x); since f(y)−f(x)≤∣f(y)−f(x)∣, such a y satisfies f(y)−f(x)<c−f(x) and therefore f(y)<c, using compatibility of the order with addition (condition 1 of Ordered Field). Let
Wc=x∈Vc⋃Bd(x,δx)
be the union of the family of open balls Bd(x,δx) indexed by x∈Vc.
The set Wc is open in (X,d). Indeed, let z∈Wc, say z∈Bd(x,δx) with x∈Vc, and put r=δx−d(x,z), which satisfies r>0 because d(x,z)<δx. If w∈Bd(z,r), then the triangle inequality of Metric Space gives d(x,w)≤d(x,z)+d(z,w)<d(x,z)+r=δx, so Bd(z,r)⊆Bd(x,δx)⊆Wc.
Moreover K∩Wc=Vc. If y∈K∩Wc then y∈Bd(x,δx) for some x∈Vc, so d(x,y)<δx and hence f(y)<c by the choice of δx, that is y∈Vc. Conversely, if x∈Vc then x∈K and d(x,x)=0<δx, so x∈Bd(x,δx)⊆Wc. Hence Vc=K∩Wc∈TK.
Step 2: f attains a maximum.
Suppose not: suppose there is no xmax∈K with f(x)≤f(xmax) for every x∈K. Then for every x∈K there is y∈K for which f(y)≤f(x) fails. For such x and y we get f(x)≤f(y) by comparability, and f(x)=f(y) because f(x)=f(y) would give f(y)≤f(x) by reflexivity; that is, f(x)<f(y).
Consider the family (Vf(y))y∈K of subsets of K indexed by the set K. Each member belongs to TK by Step 1, and the family covers K: given x∈K, there is y∈K with f(x)<f(y), and then x∈Vf(y). Since (K,TK) is compact, Compact Topological Space and Compact Subset provides a finite subset J⊆K with
K⊆y∈J⋃Vf(y),
where a union indexed by the empty set is empty. Since K is nonempty, J is nonempty. Being finite and nonempty, J has n elements for some natural number n, so there is a bijection β:[n]→J.
Apply Greatest Element of a Finite Family in a Totally Ordered Set to the set R with its total order and to the n-tuple in R whose k-th component is f(β(k)): there is j∈[n] with f(β(k))≤f(β(j)) for every k∈[n]. Put y∗=β(j), an element of J⊆K. Then y∗∈Vf(y) for some y∈J, that is f(y∗)<f(y), so f(y∗)≤f(y) and f(y∗)=f(y). Since β maps [n] onto J, we have y=β(k) for some k∈[n], whence f(y)=f(β(k))≤f(β(j))=f(y∗). Antisymmetry of the total order now gives f(y∗)=f(y), a contradiction.
Hence the supposition was false, and there exists xmax∈K such that f(x)≤f(xmax) for every x∈K.
Step 3: f attains a minimum.
Let g:K→R be defined by g(x)=−f(x), the additive inverse in R. For x,y∈K we have g(y)−g(x)=−(f(y)−f(x)), hence ∣g(y)−g(x)∣=∣f(y)−f(x)∣, so g satisfies the same continuity hypothesis as f, with the same δ for each x and ε. By Step 2 applied to g there is xmin∈K with g(x)≤g(xmin) for every x∈K, that is −f(x)≤−f(xmin). Adding f(x)+f(xmin) to both sides and using compatibility of the order with addition (condition 1 of Ordered Field) gives f(xmin)≤f(x) for every x∈K.
Steps 2 and 3 together give the assertion.