I guess I found the counterexample myself. Consider the following function defined on $[0,1]$.
$$f(x) = \left \{ \begin{array}{ll} 2x & \mbox{ if } 0 \leq x < 0.5 \\
0.5 & \mbox{ if } 0.5 \leq x \leq 1 \end{array} \right .
$$
The sup of the function is $1$, but there is no point in the domain $[0,1]$ that 'attains' this point.
Let us start with
$$h(x) = \begin{cases}
\hphantom{-}4(x+2) &, -2\leqslant x < -\frac{3}{2}\\
-4(x+1) &, -\frac{3}{2} \leqslant x < -1\\
-4(x-1) &,
\hphantom{-}\; 1 \leqslant x < \frac{3}{2}\\
\hphantom{-} 4(x-2) &,
\hphantom{-}\frac{3}{2}\leqslant x < 2\\
\qquad 0 &,
\hphantom{-} \text{ otherwise.} \end{cases}$$
For $c > 0$, let
$$h_c(x) = c\cdot h(c\cdot x).$$
Then $h_c$ is continuous, and $\int_{-\infty}^0 h_c(x)\,dx = 1$ as well as $\int_{-\infty}^\infty h_c(x)\,dx = 0$. Now let
$$g(x) = \sum_{n=1}^\infty h_{5^n}\left(x-n-\frac{1}{2}\right).$$
Every $g_n(x) = h_{5^n}\left(x-n-\frac12\right)$ vanishes identically outside the interval $[n,n+1]$, so $g$ is continuous, and
$$f(x) = \int_0^x g(t)\,dt$$
is well-defined and continuously differentiable.
Furthermore, $f(x) \equiv 0$ on every interval $\left[n, n+\frac{1}{2} - \frac{2}{5^n}\right]$, and $f(x) \equiv 1$ on every interval $\left[n+\frac{1}{2}-\frac{1}{5^n}, n+\frac{1}{2}+\frac{1}{5^n}\right]$. Thus on
$$X = \bigcup_{n=1}^\infty \left(\left[n, n+\frac{1}{2} - \frac{2}{5^n}\right] \cup \left[n+\frac{1}{2}-\frac{1}{5^n}, n+\frac{1}{2}+\frac{1}{5^n}\right]\right)$$
we have $f' \equiv 0$, so the derivative is bounded, but
$$f\left(n+\frac{1}{2}-\frac{1}{5^n}\right) - f\left(n+\frac{1}{2}-\frac{2}{5^n}\right) = 1$$
for all $n$, while the distance between the two points is $5^{-n}$ which becomes arbitrarily small, so $f$ is not uniformly continuous on $X$.
If the sentence
Note that $X$ cannot be an interval, a disjoint union of intervals, nor can it be a discrete set.
was meant to forbid a construction as above where $X$ is a disjoint union of intervals, we can obey the letter of the law (but not the spirit) by adding an arbitrary subset of $(-\infty,0)$ that is not a union of disjoint intervals.
Best Answer
Let $\langle X,d\rangle$ be any non-compact metric space, not necessarily a subspace of $\Bbb R$. Then $X$ is not countably compact, so $X$ has a countably infinite closed discrete subset $D=\{x_n:n\in\Bbb N\}$. Define
$$f:D\to\Bbb R:x_n\mapsto\begin{cases} 1-2^{-n},&\text{if }n\text{ is even}\\ -1+2^{-n},&\text{if }n\text{ is odd}\;. \end{cases}$$
By the Tietze extension theorem there is a continuous $g:X\to[-1,1]$ such that $g\upharpoonright D=f$. Every metric space is perfectly normal, so $D$ is a zero set, and there is a continuous $\varphi:X\to[0,1]$ such that $\varphi(x)=0$ if and only if $x\in D$. Let $\psi:X\to[0,1]:x\mapsto 1-\varphi(x)$, and let
$$h:X\to\Bbb R:x\mapsto g(x)\psi(x)\;;$$
then $h$ is continuous, $\inf_{x\in X}h(x)=\inf_{x\in D}h(x)=-1$, $\sup_{x\in X}h(x)=\sup_{x\in D}h(x)=1$, and $h[X]\subseteq(-1,1)$, so $h$ attains neither its supremum nor its infimum.
Added: I’ve been asked to say something about how I thought of this construction. That’s a bit difficult, since the pieces were all just lying about in my head and came together without much effort on my part, but I’ll try. I know that countable compactness and compactness are equivalent for metric spaces, so I know right away that a non-compact metric space has a countably infinite closed discrete subset $D$. Because $D$ is discrete, any function from it to $\Bbb R$ will be continuous so I’ll begin by defining a bounded function from $D$ to $\Bbb R$ that doesn’t attain its supremum or infimum. There are may such; I chose to use the function $f$ above, but any other would have worked equally well.
This particular function $f$ takes values in $(-1,1)$ and has $-1$ and $1$ as its unattained extrema. If I can extend $f$ to a continuous $h:X\to(-1,1)$, I'll be done, so what do I know about extending continuous functions? Since $D$ is closed, the Tietze extension theorem applies, and it almost does what I want: it guarantees the existence of a continuous $g:X\to\Bbb R$ with the same extrema as $f$, which implies that $g[X]\subseteq[-1,1]$. Now I just have to make sure not to hit $-1$ or $1$. I can’t do that with $g$ itself, but if I can find a continuous function $\psi:X\to[0,1]$ that is $1$ only on $D$, the product $g\psi$ is guaranteed so map $X\setminus D$ into $(-1,1)$, and I’ll be home free. That requires that $D$ be a zero set in $X$, but I know that that’s true of every closed set in a metric space, so I’m done.
By the way, here’s an example to show that the result does not hold for arbitrary non-compact spaces. The ordinal space $\omega_1$ of all countable ordinals with the order topology is a standard example of a countably compact space that isn’t compact, so in particular it’s not metric. It happens that every continuous $f:\omega_1\to\Bbb R$ is eventually constant: there is an $\alpha_f<\omega_1$ such that $f(\alpha)=f(\alpha_f)$ whenever $\alpha_f\le\alpha<\omega_1$. The set $[0,\alpha_f]$ is a compact subset of $\omega_1$, so $f\upharpoonright[0,\alpha_f]$ attains its extrema, and $f(\alpha)=f(\alpha_f)$ for $\alpha\in\omega_1\setminus[0,\alpha_f]$, so $f$ has the same extrema as $f\upharpoonright[0,\alpha_f]$ and obviously attains them.