For better clarity I will here be using the notation $A_0$ and $A_1$, instead of $A$ and $A'$, to denote the outcomes for different measurement setups, and the same with $B$.
This means that $A_x$ is the random variable describing the possible measurement outcomes on Alice's side when the measurement choice is $x$, and similarly for $B_y$ describing Bob's outcomes when his measurement choice is $y$. We will label the two possible measurement choices with $x,y\in\{0,1\}$, and the possible measurement outcomes with $\pm1$, meaning $A_x,B_y\in\{-1,1\}$.
The expectation values are defined as
$$
E(A_xB_y)\equiv\int d\lambda\, q(\lambda)\, E_\lambda(A_xB_y)
=\int d\lambda\, q(\lambda)\sum_{ab} ab \,p(ab|xy,\lambda),
$$
where $q(\lambda)$ is the probability of the hidden variable having the value $\lambda$, and $p(ab|xy,\lambda)$ is the conditional probability of getting the outcomes $a$ and $b$ given the measurement settings $x$ and $y$ and hidden variable $\lambda$.
The locality assumption (plus assumption of independence of measurement choices) is embedded in the following factorization for $p$:
$$p(ab|xy,\lambda)=p(a|x,\lambda)p(b|y,\lambda).\tag A$$
This relation is needed to have $E_\lambda(A_x B_y)=E_\lambda(A_x)E_\lambda(B_y)$, so that
\begin{aligned}
E(A_0B_0)+E(A_0B_1)
&=\int d\lambda \,q(\lambda)[E_\lambda(A_0)E_\lambda(B_0)+E_\lambda(A_0)E_\lambda(B_1)] \\
&= \int d\lambda \,q(\lambda)E_\lambda(A_0)[E_\lambda(B_0)+E_\lambda(B_1)],
\end{aligned}
where
$$E_\lambda(A_x)\equiv\sum_a a \,p(a|x,\lambda),\quad
E_\lambda(B_y)\equiv\sum_b b \,p(b|y,\lambda).$$
Without locality assumption, the above would not hold.
An analogous argument leads to
$$E_\lambda(A_1B_0)-E_\lambda(A_1B_1) = E_\lambda(A_1)[E_\lambda(B_0)-E_\lambda(B_1)].$$
The conclusion is now straightforward from here. Define
$$S_\lambda\equiv E_\lambda(A_0B_0)+E_\lambda(A_0B_1)+E_\lambda(A_1B_0)-E_\lambda(A_1B_1).$$
Then, using the above equalities, we have
$$S_\lambda= E_\lambda(A_0)[E_\lambda(B_0)+E_\lambda(B_1)]
+ E_\lambda(A_1)[E_\lambda(B_0)-E_\lambda(B_1)].$$
The triangle inequality, together with the fact that, by definition of the numbers we are attaching to the possible outputs, we have $0\le \lvert E_\lambda(A_i)\rvert \le 1$, now gives
$$\lvert S_\lambda\rvert\le \lvert E_\lambda(B_0)+E_\lambda(B_1)\rvert
+ \lvert E_\lambda(B_0) - E_\lambda(B_1)\rvert = 2\max(|E_\lambda(B_0)| , |E_\lambda(B_1)|).$$
The full $S$ is now defined by averaging $S_\lambda$ over the hidden variable $\lambda$, and because a convex mixture of numbers in $[-2,2]$ remains in $[-2,2]$, we reach the conclusion:
$$\lvert S\rvert\le 2.$$
But we could do the same trick if A depends on b too, so where is locality used?
No, you could not.
If the outcome of $A$ directly depends on $B$, then (A) does not hold, thus the probabilities do not factorize ($E(AB)\neq E(A)E(B)$), thus $E(A_0B_0)+E(A_0B_1)\neq E(A_0(B_0+B_1))$, thus the CHSH argument cannot be applied.
Aren't expectations always linear?
Indeed they are. The locality assumption is needed not for the linearity but to factorize the probabilities/expectation values, in order to put them into the form $E_\lambda(A_0)[E_\lambda(B_0)+E_\lambda(B_1)]$, at which point the CHSH argument applies.