Emilio Pisanty and Eckhard Giere have already given discontinuous, piecewise constant counterexamples in their answers. Here we provide for-the-fun-of-it a smooth infinitely-many-times-differentiable counterexample $f\in C^{\infty}(\mathbb{R})$ of a square integrable function $f:\mathbb{R} \to [0,1]$ that does not satisfy $\lim_{|x|\to \infty}f(x)=0$. Our counterexample is
$$\tag{1}
f(x)~:=~ e^{- g(x)} ~\in ~]0,1], \qquad g(x)~:=~x^4 \sin^2 x~\in ~[0,\infty[. $$
Intuitive idea: If we imagine $x$ as a time variable, then the function $f$ returns periodically to its maximum value
$$\tag{2} f(x) =1 \quad\Leftrightarrow\quad g(x) =0 \quad\Leftrightarrow\quad \frac{x}{\pi}\in \mathbb{Z} ,$$
but spends most if its time close to the $x$-axis in order to be square integrable.
Proof: We leave a detailed rigorous epsilon-delta mathematical proof to the reader, but a sketched heuristic proof goes like this. For each very large integer $|n|\gg 1$, define a shifted variable
$$\tag{3} y~:=~x-\pi n.$$
For the fixed integer $n\in\mathbb{Z}$, always assume from now on that the $y$-variable belongs to the interval
$$\tag{4} |y|~\leq~ \frac{\pi}{2}.$$
For $|y|\ll\frac{\pi}{2}$ very small, we may approximate $g(x) \approx (\pi n)^4y^2$, so that in the interval (4), we have
$$\tag{5} g(x)~\lesssim~ \pi^4 |n| \quad \Leftrightarrow\quad |y| ~\lesssim~ |n|^{-\frac{3}{2}}.$$
Thus we may form a square integrable majorant function $h\geq f$ (outside a compact region on the $x$-axis) by defining
$$\tag{6} h(x)~:=~\left\{\begin{array}{lcl} 1 &{\rm for}& |y| ~\lesssim~ |n|^{-\frac{3}{2}}, \cr
e^{-\pi^4 |n|}&{\rm for}& |n|^{-\frac{3}{2}}~\lesssim~ |y| ~\leq~ \frac{\pi}{2}, \end{array} \right. \qquad |n|\gg 1. $$
The function $h\in {\cal L}^2(\mathbb{R})$ is square integrable on the whole $x$-axis, since
$$\tag{7} \sum_{n\neq 0} |n|^{-\frac{3}{2}} ~<~ \infty$$
and
$$\tag{8} \pi \sum_{n\in\mathbb{Z}}e^{-2\pi^4 |n|}~<~\infty$$
are convergent series.