Let $(X_n)_{n \geq 1}$ a sequence of real random variables, let $N$ be a random variable taking integer values , independant of $(X_n)$
We define $S_N$ on $\Omega$ by $S_N(\omega) = 0$ if $N(\omega ) = 0$
and $S_N(\omega) = \sum^{N(\omega)}_{i=1} X_i(\omega)$ if $N(\omega) \geq 1$
We want to prove $S_N$ is a random variable
$\{ \omega; S_N(\Omega) \in A \} = \cup_{k \in N} \{ \omega ; \sum^{N(\omega)}_{i=1} X_i(\omega) \in A$ and $N(\omega)=k \}$$\dots (1)$
$=\cup_{k \in N} \{ \omega ; \sum^k_{i=1} X_i(\omega) \in A$ and $N(\omega)=k \}$
I can understand then how it verifies the random variable definition since $N$ and $\sum^k_{i=1} X_i(\omega)$ are random variables, so the events $\{\sum^k_{i=1} X_i(\omega) \in A \}$ and $\{ N(\omega) = k \}$ are in $\mathcal{F}$ so is their intersection, and their union since the countable union of elements of $\mathcal{F}$ is in $\mathcal{F}$, which proves that $S_N$ is a random variable. But I don't get how we came up with the equality (1)
$S_N(\omega)$ is defined as $\sum_{i=1}^{N(\omega)} X_i(\omega)$, with the understanding that the empty series is equal to zero.
Random variable $N$ has a distribution supported over integer values.
So $\sum_{i=1}^{N(\omega)}X_i(\omega)\in A$ will be true for some integer $k$ where $N(\omega)=k$ and $\sum_{i=1}^{N(\omega)} X_i(\omega)\in A$.
Of course, when $N(\omega)=k$ is the case, then $\sum_{i=1}^{N(\omega)} X_i(\omega)\in A$ will be equivalent to $\sum_{i=1}^{k} X_i(\omega)\in A$.
Everything else is just the definition of arbitrary union.
$$\begin{align}\{\omega\in\Omega: S_N(\omega)\in A\} &=\{\omega\in\Omega: \sum_{i=1}^{N(\omega)}X_i(\omega)\in A\}\\[1ex]&=\{\omega\in\Omega: \exists{k\in\Bbb N}~.[N(\omega)=k\wedge \sum_{i=1}^{N(\omega)}X_i(\omega)\in A]\}\\[1ex]&=\{\omega\in\Omega: \exists{k\in\Bbb N}~.[N(\omega)=k\wedge \sum_{i=1}^kX_i(\omega)\in A]\}\\[1ex]&=\bigcup_{k\in\Bbb N}\{\omega\in\Omega:N(\omega)=k\wedge\sum_{i=1}^kX_i(\omega)\in A\}\end{align}$$