I am looking at a proof and there is a step I don't understand well. Sorry if this post seems disorganized, but I'm not sure how to ask this question in any other way.
Statement: Let $R$ be a commutative ring with unity. Let $M\cong \oplus_{x\in X}R$ and $N\cong \oplus_{y\in Y}R$ as $R$-modules. Then $M\cong N \Rightarrow |X|=|Y|$.
Proof: Let $P$ be a maximal ideal of $R$. Define $k:=R/P$. Then $k$ is a field and an $R$-algebra. We see that:
$$M\otimes_R k \\ \cong (\oplus_{x\in X}R)\otimes_R k\\ \cong \oplus_{x\in X} (R\otimes_R k) \\ \cong \oplus_{x\in X}k.$$
Similarly, $N\otimes_R k \cong ... \cong \oplus_{y\in Y}k$.
But $M\otimes_R k$ and $N\otimes_R k$ are $k$-modules, and $M\otimes_R k\cong N\otimes_R k$. Since $k$ is a field, they are vector spaces. Since two vector spaces being isomorphic implies that their bases have the same cardinality, we have that $|X|=|Y|$.
My question(s):
Why can we assert that $M\cong N$ as $R$-modules means that $M\otimes_R k\cong N\otimes_R k$ as $k$-modules? Does this work for a general $R$-algebra $S$? That is, if $M\cong N$ as $R$-modules, is it the case that $M\otimes_R S\cong N\otimes_R S$ as $S$-modules?
What you are describing is an instance of a much more general situation, which in my opinion is worth knowing, so I will try to elaborate:
What makes this work is the fact that the assignment $M\mapsto M\otimes_R k$ is a functor. What this means is the following:
A functor from the category of $R$-modules $_R\operatorname{Mod}$ (although not entirely accurate, you can think of this as the "set" of $R$-modules, and between each two $R$-modules you have the set of $R$-module homomorphisms between them) to the category of $S$-modules $_S\operatorname{Mod}$ is an assignment $F:_R\operatorname{Mod}\to_S\operatorname{Mod}$ such that
This is a very general notion, and almost every construction you encounter in algebra is in some sense a functor.
Now what is very nice about functors is that they preserve isomorphism. That is, if you have isomorphic $R$-modules $M\cong N$, then you get isomorphic $S$-modules $FM\cong FN$. Indeed, $M\cong N$ means that we have mutually inverse $R$-module homomorphisms $f:M\to N$ and $g:N\to M$ (i.e. $g\circ f=\operatorname{id}_M$ and $f\circ g=\operatorname{id}_N$). By 3. and 4., we then obtain that the $S$-module homomorphisms $Ff:FM\to FN$ and $Fg:FN\to FM$ are also mutually inverse (i.e. $Fg\circ Ff=\operatorname{id}_{FM}$ and $Ff\circ Fg=\operatorname{id}_{FN}$). That is, $FM$ and $FN$ are isomorphic.
Now what you should check is that $M\mapsto M\otimes_R k$ is indeed a functor. More concretely, given a map $f:M\to N$ you should construct a $k$-module homomorphism $f\otimes_R k:M\otimes_R k\to M\otimes_R k$, such that 3. and 4. are satisfied. Once you have this, everything is automatic.