Expanding $(x + i y)^n$ with the binomial formula gives
$$(x + i y)^n = \sum_{k = 0}^n {n \choose k} x^{n - k} (iy)^k $$
Now, we can split the sum into real and imaginary parts using $i^{2 k} = (-1)^k$ and $i^{2 k + 1} = (-1)^k i$:
$$(x + i y)^n = \underbrace{\sum_{\begin{array}{c}k = 0 \\ k \equiv 0 \pmod 2\end{array}}^{n} (-1)^{k / 2} {n \choose k} x^{n - k} y^k}_{\Re [(x + iy)^n]} + i \underbrace{\sum_{\begin{array}{c}k = 0 \\ k \equiv 1 \pmod 2\end{array}}^{n} (-1)^{(k - 1) / 2} {n \choose k} x^{n - k} y^k}_{\Im [(x + iy)^n]} .$$
One can eliminate the modulus conditions $k \equiv r \pmod 2$ in the summations by reindexing. For example,
$$\Re[(x + iy)^n] = \sum_{j = 0}^{\lfloor\frac{n}{2}\rfloor} (-1)^j {n \choose 2j} x^{n - 2j} y^{2j} .$$
These formulas tell us, by the way that we can read the coefficients off of Pascal's Triangle. For each $n$, the coefficients of the real part are given by looking at the $n$th row of Pascal's Triangle (here, we take the initial row, $1$ to be the $0$th row), and reading off every other entry with alternating signs, starting with the first. For the imaginary part, we do the same, but starting with the second entry. For example, for $n = 4$, the corresponding row is
$$\matrix{\color{blue}{1} & \color{red}{4} & \color{blue}{6} & \color{red}{4} & \color{blue}{1}} .$$
Our algorithm then tells us that we can read off these coefficients to give
$$\Re[(x + iy)^4] = + \color{blue}{1} x^4 - \color{blue}{6} x^2 y^2 + \color{blue}{1} y^4 = x^4 - 6 x^2 y^2 + y^4$$
and
$$\Im[(x + iy)^4] = + \color{red}{4} x^3 y - \color{red}{4} x y^3 = 4 x^3 y - 4 x y^3 .$$