To spill the beans: this happens if and only if both $G_1$ and $G_2$ are torsion groups, and for every $x\in G_1$ and $y\in G_2$, $\gcd(|x|,|y|)=1$.
Once you figure out the statement one can prove it directly, as I do below; but I arrived at it using my favorite "hammer" to deal with subgroups of $G_1\times G_2$, Goursat's Lemma. So I leave the development that led to the result below.
The subgroups of $G_1\times G_2$ are described by Goursat's Lemma. Each such subgroup corresponds to a $5$-tuple $(H_1,N_1,H_2,N_2,\varphi)$, where
- $H_i$ is a subgroup of $G_i$, $i=1,2$.
- $N_i\triangleleft H_i$, $i=1,2$.
- $\varphi$ is an isomorphism $\varphi\colon \frac{H_1}{N_1}\to \frac{H_2}{N_2}$.
Such a tuple corresponds to the subgroup
$$\{ (x,y)\in G_1\times G_2\mid x\in H_1, y\in H_2, \varphi(xN_1)=yN_2\}.$$
So the question is: when will such a subgroup be equal to $H_1\times H_2$? That will happen if and only if $\varphi(xN_1)=yN_2$ for every $x\in H_1$ and every $y\in H_2$. This in turn means that $N_1=H_1$ and $N_2=H_2$.
If $G$ is a group, Let us denote by $\mathbf{HS}(G)$ the class of all groups that are isomorphic to a homomorphic image of a subgroup of $G$ (that is, isomorphic to $H/N$ for some $H\leq G$ and $N\triangleleft H$).
Thus, we have:
Theorem. Let $G_1$ and $G_2$ be two groups. The following are equivalent:
- Every subgroup of $G_1\times G_2$ is equal to $H_1\times H_2$ for some $H_1\leq G_1$ and $H_2\leq G_2$.
- The only group that the classes $\mathbf{HS}(G_1)$ and $\mathbf{HS}(G_2)$ have in common is the trivial group.
For finite groups, the condition is very strong:
Corollary 1. Let $G_1$ and $G_2$ be two finite groups. The following are equivalent:
- Every subgroup of $G_1\times G_2$ is equal to $H_1\times H_2$ for some $H_1\leq G_1$ and $H_2\leq G_2$.
- $\gcd(|G_1|,|G_2|)=1$.
Proof. Since the order of an element of $\mathbf{HS}(G)$ divides the order of $G$ when $G$ is finite, condition 2 implies condition 2 of the theorem. If $|G_1|$ and $|G_2|$ are not relatively prime, let $p$ be a prime dividing both, and let $x\in G_1$, $y\in G_2$ be elements of order $p$. The subgroup $\langle (x,y)\rangle$ is cyclic of order $p$ with both projections nontrivial, and so it is not of the form described in 1. $\Box$
Corollary 2. Let $G_1$ and $G_2$ be nontrivial groups, and assume that $G_1$ is infinite.
- If $G_1$ is not torsion, then there are subgroups of $G_1\times G_2$ that are not of the form $H_1\times H_2$ with $H_i\leq G_i$.
- If both $G_1$ and $G_2$ are torsion, then every subgroup of $G_1\times G_2$ is of the form $H_1\times H_2$ with $H_i\leq G_i$ if and only if there is no prime $p$ such that both $G_1$ and $G_2$ have elements of order $p$. Equivalently, if $x\in G_1$ and $y\in G_2$, then $\gcd(|x|,|y|)=1$.
Proof. 1. Let $x\in G_1$ be of infinite order. Let $y\in G_2$ be an arbitrary nontrivial element. Then there is a quotient of $\langle x\rangle$ isomorphic $\langle y\rangle$, and the conclusion follows from the theorem.
- If $G_1$ and $G_2$ have elements of order $p$, then the result follows as in Corollary 1. Otherwise, let $H_i\leq G_i$ and $N_i\triangleleft H_i$ with $H_1/N_1$ isomorphic to $H_2/N_2$. Every element of $H_1/N_1$ has order dividing the order of an element of $G_1$; every element of $H_2/N_2$ has order dividing the order of an element of $G_2$. But the orders of elements of $G_1$ and of $G_2$ are coprime, so both $H_1/N_1$ and $H_2/N_2$ are trivial. $\Box$
In summary the promised result:
Theorem. Let $G_1$ and $G_2$ be groups. Then every subgroup of $G_1\times G_2$ is of the form $H_1\times H_2$ with $H_i\leq G_i$ if and only if both $G_1$ and $G_2$ are torsion groups, and for every $x\in G_1$ and $y\in G_2$, $\gcd(|x|,|y|)=1$.
This follows directly from the development done above, but here is a direct, constructive, elementary proof that does not invoke Goursat's Lemma.
Proof. For the "if" directly, if $S$ is a subgroup of $G_1\times G_2$ and $(x,y)\in S$, then since $\gcd(|x|,|y|)=1$ there exist $a$ and $b$ such that $a\equiv 0\pmod{|x|}$, $a\equiv 1\pmod{|y|}$, and $b\equiv 1\pmod{|x|}$, $b\equiv 0\pmod{|y|}$. Then $(x,y)^a = (e,y)$ and $(x,y)^b=(x,e)$, so both $(x,e)$ and $(e,y)$ are in $S$. Thus, $S=\pi_1(S)\times\pi_2(S)$.
For the "only if" clause, let $x\in G_1$ and $y\in G_2$; we must prove they are both torsion elements and their orders are relatively prime. Without loss of generality assume that $x\neq e$ (if both are trivial, they desired conclusion follows). Consider the group $H=\langle (x,y)\rangle$. Because $H=H_1\times H_2$ for some subgroups $H_1\leq G_1$ and $H_2\leq G_2$, we must have $(x,e)\in H$. So there exists an integer $k$ such that $(x,y)^k = (x^k,y^k)=(x,e)$. In particular, $y$ is of finite order that divides $k$; and $x$ is also of finite order and $k$ is congruent to $1$ modulo the order of $x$. That means that the order of $x$ is relatively prime to $k$, and hence to its divisor the order of $y$; so $x$ and $y$ are torsion elements and $\gcd(|x|,|y|)=1$, as claimed. $\Box$