As Francois Ziegler answered, for a locally compact group $G$, the Bohr compactification of $G$ is a compactification in the usual sense iff $G$ is compact. This is true with no further restrictions.

Recall that the forgetful functor from compact groups to topological groups has a left adjoint functor, denoted here $b$. For any topological group $G$, the identity in $\text{Hom}(bG,bG)$ corresponds to an element of $\text{Hom}(G,bG)$ called the unit, $u:G\to bG$.
It is easy to check that $u(G)$ is dense in $bG$ (this is usually seen directly from the construction of $b$, but also follows from the abstract nonsense).
The homomorphism $u:G\to bG$ is known as the
*Bohr compactification* of $G$.
This terminology is a bit unfortunate, as $u$ is not a compactification in the sense usually used in topology, unless $G$ was compact to begin with
(in fact, $u$ is rarely injective - groups for which $u$ is injective are called "maximally almost periodic").

By a "compactification in the usual sense" of a locally compact space $X$ one means a continuous map $f:X\to Y$ where $f(X)$ is dense in $Y$ and $f:X\to f(X)$ is a homeomorphism. In that case $f(X)$ is necessarily open in $Y$ (this an easy exercise).

In case $X$ and $Y$ were groups and $f$ a group homomorphism, $f(X)$ was also closed, as any open subgroup is closed, thus $f(X)=Y$ by density. So $X$, being homeomorphic to $Y$, must have been compact to begin with.

More generally, let me note that any locally compact subgroup of any topological group is necessarily closed (and if the ambient group is locally compact, a subgroup is locally compact iff it is closed).

1more comment