André Henriques answer can easily be improved to $C^\infty$ by considering $e^{-1/d(x)}$ (extended by $0$ on the closed set) with $d(x)$ denoting the distance of $x$ to the closed set.