Note that for each $a\in A,$ the set $\{a\}\times B$ has cardinality $|B|.$ (Why?) Further note that the sets $\{a\}\times B$ (where $a\in A$) comprise a partition of $A\times B.$ Hence, since there are $|A|$ elements of $A,$ we have $$|A\times B|=\left|\bigcup_{a\in A}\{a\}\times B\right|=\sum_{a\in A}\bigl|\{a\}\times B\bigr|=\sum_{a\in A}|B|=|A||B|.$$
Observe that this never assumes that $|A|=|B|,$ and applies for any sets $A,B,$ regardless of cardinality. An example for you to consider is when $A=\{x,y\}$ and $B=\{1,2,3\}.$ You should be able to find $A\times B$ explicitly, and see that it has $6$ elements.