I have read that given a $\mathbb{Z}$-module $M$, the maximal number of $\mathbb{Z}$-linear independent elements is given by $\operatorname{rank}M=\dim_\mathbb{Q}(\mathbb{Q}\otimes_\mathbb{Z}M)$. However I do not understand, how $\mathbb{Q}\otimes_\mathbb{Z}M$ becomes a $\mathbb{Q}$-module. A priori, both $\mathbb{Q}$ and $M$ are only $\mathbb{Z}$-modules and tensoring over $\mathbb{Z}$ should yield another $\mathbb{Z}$-module. What am I missing here?
After understanding the above I would also like to know, if we can replace $\mathbb{Z}$ by other (commutative rings)? Or do we need domains here, so that we can build a field of fractions?
Consider the multiplicative set $S= \mathbb Z\setminus \{0\}$. Then $$\mathbb Q\simeq S^{-1}\mathbb Z.$$ Now, in general you have that, if $M$ is an $A$-module and $S$ is a multiplicative set of $A$, then $$S^{-1}A\otimes_AM \simeq S^{-1}M, $$ that is naturally a $S^{-1}A$-module.
This shows that this definition of rank of an $A$-module holds whenever $A$ is a domain. If $A$ is not a domain i know that one of the possible definitions of rank of a module $M$ (I read it in some book that i don't remember) is
$$rank(M)=\max\left\{rank_{A/p}(M/ pM) \;|\; p \mbox{ minimal prime ideal of } A \right\}.$$