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 $\frak g$, $\frak h$ , $\frak k$ be their Lie algebras.
The complexifications
${\frak g}_{\Bbb C}$, ${\frak h}_{\Bbb C}$, ${\frak k}_{\Bbb C}$
are algebraic Lie algebras, i.e. Lie algebras of complex algebraic groups.
Let ${\frak l}_{\Bbb C}$ be the Lie subalgebra of ${\frak g}_{\Bbb C}$ generated by Lie subalgebras ${\frak h}_{\Bbb C}$ and ${\frak k}_{\Bbb 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 ${\frak l}_{\Bbb C}$ is algebraic,
i.e. it is the Lie algebra of a unique connected complex algebraic subgroup
$\mathbf{L}_{\Bbb C}\subset \mathbf{G}_{\Bbb C}$. Clearly $\mathbf{L}_{\Bbb C}$ is defined over $\Bbb R$, i.e. comes from some real
algebraic subgroup $\mathbf{L}\subset \mathbf{G}$.
Set $L=\mathbf{L}(\Bbb 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 $\frak l$ of $L$ is generated by the subalgebras $\frak h$ and $\frak 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 $\frak l$ is generated by $\frak h$ and $\frak k$, one can easily see that for any element $A\in \frak 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.

fixed$N$. In that case, $L$ would be a continuous image of the compact space $(H\cup K)^N$, hence compact. I'm not sure if $L$ can always be written in this form, but it seems reasonable. $\endgroup$