Let $G=\mathbb{Z}^n\times F_m$ where $F_m$ denotes the free group of rank $m$. Consider the abelianization of $G$, i.e., the surjective map
$$
\pi:G\twoheadrightarrow G/[G,G]\simeq\mathbb{Z}^{n+m}.
$$
Since this map is surjective, if $\{g_1,\dots,g_k\}$ generate $G$, then $\pi(g_1),\dots,\pi(g_k)$ generate $\mathbb{Z}^{n+m}$. Since the rank of $\mathbb{Z}^{n+m}$ is $n+m$, this implies that $k\geq n+m$.
On the other hand, we can write down a generating set of $G$ consisting of $n+m$ generators as follows: $\mathbb{Z}^n$ is generated by $n$ coordinate vectors $\{e_1,\dots,e_n\}$ and $F_m$ is, by definition, generated by $m$ elements $\{f_1,\dots,f_m\}$. Therefore, $G$ is generated by $\{e_1,\dots,e_n,f_1,\dots,f_m\}$.
Since we have shown that it is possible to generate $G$ with $n+m$ generators and, in addition, that any generating set for $G$ must have at least $n+m$ elements, it follows that exactly $n+m$ generators is the best possible, i.e., the rank is $n+m$.
For the further generalization, combining this argument with the fundamental theorem of finitely generated abelian groups should provide exactly what you're looking for.
The idea is the same used to get a basis for the dual space of a vector space $V$ of finite dimension.
Let us define the homomorphisms $g_i^*\colon G\to \mathbb{Z}$ such that $g_i^*(g_j):=\delta_{ij}$. Then given $f\in Hom(G,\mathbb{Z})$ you have that
$ f=\sum_{g_i}f(g_i)\cdot g_i^*$
Now suppose that $\sum_{g_i}a_ig_i^*=0$. Then $\left(\sum_{g_i}a_ig_i^*\right)(g_j)=a_j=0$ for each $j$. Hence $Hom(G,\mathbb{Z})$ is a free abelian group of rank $n$.
Best Answer
As has been noted in the comments, there is some linear algebra going on under the hood. That is maybe a hint that we should view this problem geometrically. To that end, I'll be calling elements of this group "vectors", and thinking of them as living in some integer lattice of $\mathbb{R}^n$. Indeed, these kinds of questions arise very naturally in the geometry of numbers.
We say a set of vectors $\{v_1, \ldots, v_i \}$ is primitive exactly when it can be extended to a basis of the lattice. So in this problem we are interested in knowing when $\{ w \}$ is primitive.
If we write $w = g_1^{n_1} g_2^{n_2} \cdots g_k^{n_k}$ in terms of the "natural" basis $\{ g_1, \ldots, g_k \}$, there turns out to be a nice combinatorial description of primitivity:
A nice computational proof can be found as Theorem $2.1$ in Magliveras, van Trung, and Wei's "Primitive Sets in a Lattice" (see here).
This turns out to be related to the comment about finding a matrix whose determinant is $\pm 1$. Indeed, the proof in the above article works by cleverly using bezout's lemma to take a (row) vector $w = (n_1, \ldots, n_k)$ and find $k-1$ new (row) vectors $v_2, \ldots, v_k$ so that when we assemble these vectors into a matrix, we find
$$ \text{det} \begin{pmatrix} - w - \\ - v_2 - \\ \vdots \\ - v_k -\end{pmatrix} = \text{gcd}(n_1, \ldots, n_k) $$
When $\text{gcd}(n_1, \ldots, n_k) = 1$ this matrix has determinant $1$, and thus is invertible as an integer matrix. But this is exactly what it means for your rows to be a basis. The linked article is nice because it shows you what the other basis elements are too.
I hope this helps ^_^