Is it possible to extend an arbitrary smooth function on a closed subset of $R^n$ to a smooth function on $R^n$?

Yes, you may extend $f$ to a $C^\infty$ function on $\mathbb R^n$.

The local conditions you give on the function $f:K\to \mathbb R$ say exactly that $f$ is a section on $K$ of the sheaf $ C^\infty_{\mathbb R^n}$, i.e. $f\in \Gamma (K, C^\infty_{\mathbb R^n})$.
This sheaf is fine, a translation of the existence of partitions of unity on $\mathbb R^n$, hence soft (see here and here ) and thus by definition of soft, this implies that the restriction map $$\Gamma (\mathbb R^n, C^\infty_{\mathbb R^n})=C^\infty (\mathbb R^n) \to \Gamma (K, C^\infty_{\mathbb R^n}):F\mapsto F\mid K$$ is surjective, answering your question in the affirmative.

Remarks
1) The closed set $K$ does not have to be compact.
In fact the result and its proof generalize word for word to the case of a closed subset of a paracompact differential manifold.
2) One could unpack everything I wrote so as to eschew the use of sheaves, but I would rather consider that the ease with which you can solve such a question is good propaganda for sheaves, which are a very easy notion anyway ( at least as long as cohomology is not introduced).

Tags:

Analysis