Hint: Here is one of many ways of constructing an example. Let $G$ be the group of permutations of the integers. Let $f$ be the permutation that takes any integer $x$ to $-x$, and $g$ the permutation that takes any integer $x$ to $1-x$.
Both $f$ and $g$ have order $2$. Now consider the permutation $gf$, meaning $f$, followed by $g$. Show that $gf$ does not have finite order.
If you prefer matrices, let $A=\begin{pmatrix}-1 &0\\0&1\end{pmatrix}$ and $B=\begin{pmatrix}-1 &1\\0&1\end{pmatrix}$.
Then $A^2$ and $B^2$ are both the identity matrix. But $BA$ has infinite order. To see this, check what $BA$ does to the vector $\begin{pmatrix}n \\1\end{pmatrix}$
Remark: For the Abelian case, we need to show closure under product and inverse. For product, note that if $a^m=e$ and $b^n=e$, then $(ab)^{mn}=a^{mn}b^{mn}=e$. Inverse is easier, since in any group, Abelian or not, the inverse of $a$ has the same order as $a$.
Writing $|a|=k$ and $|b|=m$ for all $k,m\in G$ makes no sense, since the order of an element is a positive integer. Also, you haven't proven $(ab)^{km}=e$ explicitly. You need to use the fact that $(ab)^n=a^nb^n$ in an abelian group.
The inverse part is straightforward; suppose $a^n=e$. Then
$$
e=a^{-n}a^n=(a^{-1})^ne\implies (a^{-1})^n=e.
$$
Hence $a^{-1}\in H$ as well.
Best Answer
Let $U$ be the set of all elements of finite order.
Therefore, $U$ is a subgroup.