The abstract subgroup generated by $H$ and $K$ is closed.
We may assume that $G$ is connected.
The groups $G$, $H$, $K$ are the groups of real points of real algebraic groups $\mathbf{G}$, $\mathbf{H}$, $\mathbf{K}$.
Let $g$, $h$ , $k$ be their Lie algebras.
The complexifications $g_C$, $h_C$, $k_C$ are algebraic Lie algebras, i.e. Lie algebras of complex algebraic groups.
Let $l_C$ be the Lie subalgebra of $g_C$ generated by Lie subalgebras $h_C$ and $k_C$.
We refer to the book: Onishchik, A. L.; Vinberg, È. B. Lie groups and algebraic groups.
Springer-Verlag, Berlin, 1990.
By Theorem 3.3.2 of this book, the Lie subalgebra $l_C$ is algebraic,
i.e. it is the Lie algebra of a unique connected complex algebraic subgroup
$\mathbf{L}_C\subset \mathbf{G}_C$. Clearly $\mathbf{L}_C$ is defined over $R$, i.e. comes from some real
algebraic subgroup $\mathbf{L}\subset \mathbf{G}$.
Set $L=\mathbf{L}(R)$.
Since $\mathbf{L}$ is connected and compact, the group of real points $L$ is connected,
see Onishchik and Vinberg, Corollary 1 of Theorem 5.2.1.
The Lie algebra $l$ of $L$ is generated by the subalgebras $h$ and $k$.
Since $H$ and $K$ are connected, $\mathbf{L}$ contains $\mathbf{H}$ and $\mathbf{K}$,
and $L$ contains $H$ and $K$.
Let $L'$ denote the abstract subgroup generated by $H$ and $K$, it is contained in $L$.
Since the Lie algebra $l$ is generated by $h$ and $k$, one can easily see that for any element $A\in l$
there exists a smooth map $\varphi$ from an interval $(-\varepsilon, \varepsilon)$ to $L$
with image contained in $L'$ and such that $d\varphi|_0=A$.
It follows that $L'$ contains an open neighborhood of $1$ in $L$.
Since $L$ is connected, we see that $L'=L$.
Thus the abstract subgroup $L'$ generated by $H$ and $K$ is closed.