Since you've mentioned Atiyah's "Intoduction to Commutative Algebra", chapter 2 in the comments I will assume we talk about module isomorphisms here (because that's what the chapter is about).
Note that it may happen that $R^n$ is ring isomorphic to $R^m$ for $n\neq m$. Actually the example in the question works just fine (the bijection later in this answer is a ring homomorphisms, but not a module homomorphism).
Suppose $R$ is a commutative ring with $1$. Further if we have the isomorphism given by $R^m \cong R^n$, then does that imply $m=n$?
This is answered here: R is a commutative ring with $1\ne 0$ and $R^m \cong R^n$, then $m=n$
If I consider $R=\mathbb{Z} \times \mathbb{Z} \times \mathbb{Z} \times \mathbb{Z} \times \mathbb{Z} \dots $, then I think $R^m=R^n$ as both $R^m$ and $R^n$ has countably infinite copies of $\mathbb{Z}$. Hence $R^m \cong R^n$ for any natural numbers $m \neq n$?
I could simply answer this question with "$R^m$ is not module isomorphic to $R^n$ for $n\neq m$ because the first part says so".
But lets have a closer look at this case. Is $R^m$ really isomorphic to $R^n$ as modules when $n\neq m$? Consider the simple case $n=1$, $m=2$. So what would $f:R^2\to R$ isomorphism look like? Well, one idea is to take a pair of sequences $(r,t)\in R^2$ and combine them into one sequence by mixing $(r_0,t_0,r_1,t_1,r_2,t_2,\ldots)$. Formally with a formula like this:
$$f(r,t)_n=\begin{cases}
r_{n/2} &\text{for even } n \\
t_{(n-1)/2}&\text{otherwise}
\end{cases}$$
Clearly $f$ is a bijection. The standard one when proving that a countable set $X$ is equinumerous with $X^2$. But is it a module homomorphism? Well, remember that the underlying ring is $\mathbb{Z}\times\mathbb{Z}\times\cdots$ itself. And so given a sequence $s\in \mathbb{Z}\times\mathbb{Z}\times\cdots$ is it true that $f(sr,st)=sf(r,t)$? I think you can easily verify that it is false, for example by considering $s_n=r_n=t_n=n$.
Anyway, no matter what function you come up with, as long as it is a bijection $R^2\to R$ it cannot be a module homomorphism (due to our general result).