Assuming $X' = X^T$, then the rank of $X^TY$ will be at most $K$ so it may not be possible to solve the system $($if, for instance, $D$ has rank $>K)$.
Now, given an $m\times n$ matrix $M$ with rank $\leqslant r$, there is an $m\times r$ matrix $C$ and an $r\times n$ matrix $R$ with $M=CR$. See 'Decomposition rank' in the linked section of this Wikipedia article.
This ensures that you can look to minimize
$$\min {\lVert D - D_K\rVert}^2, \tag{$*$}$$
where $D_K\in \Bbb R^{N\times N}$ has rank at most $K$.
If you do find a minimizer, it is guaranteed that you can write $D_K = X^TY$ with matching dimensions.
The good news is that the set $S_K$ of matrices with rank at most $K$ is closed.
See, for instance, this answer.
It is essentially a consequence of the rank being the largest size of a non-vanishing minor (also described in the linked section of the Wikipedia article above).
So the problem in $(*)$ is about minimizing the distance between a compact set $\big(\{D\}\big)$ and a closed set $(S_K)$.
It follows that the infimum is attained, that is, there is some $D_K$ which minimizes $(*)$.
The set $S_K$, however, is not convex, and indeed its convex hull is the entire space.
I don't know how you'd go about finding the minimizer algorithmically, it is out of my area of expertise.