I'm pretty sure this is a very basic thing, but my background is in physics and I have never previously done any number theory.
We have as a theorem that for an algebraic number field $K$, $\alpha \in K$ is an algebraic integer if and only if its minimal polynomial in $\mathbb{Q}$ has coefficients in $\mathbb{Z}$. The minimum polynomial for $\alpha = a + b \sqrt{d}$ in the field $\mathbb{Q}[\sqrt{d}]$ is then:
$$ (X - (a + b \sqrt{d}))(X - (a - b\sqrt{d})) = X^{2} - 2aX + (a^{2} - b^{2}d) $$
Therefore $\alpha$ is an algebraic integer $\iff 2a \in \mathbb{Z}, a^{2}-b^{2}d \in \mathbb{Z}$.
Fine, but why is this claim true?
$$ d = 2,3 \; \bmod \; 4 \implies \mathscr{O}_{\mathbb{Q}[\sqrt{d}]} = \mathbb{Z}[\sqrt{d}] $$ $$ d= 1\; \bmod \; 4 \implies \mathscr{O}_{\mathbb{Q}[\sqrt{d}]} = \mathbb{Z} \bigg[\frac{1+\sqrt{d}}{2}\bigg]$$
Is this somehow related to the elementary theorem about when the sum of two squares is an integer?
You got $n=2a \in\Bbb Z$ and $a^2-db^2\in \Bbb Z.$ These imply $m=2b\in\mathbb Z$ and $n^2-dm^2\in\Bbb Z.$ $n^2$ and $m^2\equiv 0$ or $1\pmod4$. If $n^2\equiv dm^2\pmod4$ and $d\equiv2 $ or $3\pmod4$, the only solution is $n^2\equiv m^2\equiv0\pmod4$ so $n\equiv m\equiv 0\pmod2$ so $a, b \in\Bbb Z$. If $d\equiv1\pmod4$ there is also the solution $n^2\equiv m^2\equiv1\pmod4$, in which case $a$ and $b$ are half odd integers.