$C_0^{\infty}(\mathbb{R})$ is dense in $L^2(\mathbb{R})$ thus exists a sequence $g_n \in C_0^{\infty}(\mathbb{R})$ such that $g_n \rightarrow f$ in $L^2(\mathbb{R})$.
Also $\int|g_n-f|^2=\int g_n^2+\int f^2 \Rightarrow \int g_n^2 \rightarrow -\int f^2$
Also $\int f^2 \leqslant \int |g_n-f|^2+ \int g_n^2$
Taking limits we have that $$2 \int f^2 \leqslant 0 \Rightarrow f^2=0$$ almost everywhere thus $f=0$ almost everywhere.
Now according to the comment below of @zhw there is an easier solution than the first one:
$$|\int g_nf-\int f^2| \leqslant \int|g_n-f||f| \leqslant \sqrt{\int|g_n-f|^2} \sqrt{\int|f|^2} \rightarrow 0$$
Thus $$0=\lim_{n \rightarrow \infty} \int g_nf=\int f^2$$
Thus $f=0$ almost everywhere.