I don't understand the following derivation:
$$ e_i = y_i - ax_i - b$$
$$ e_i = (y_i - \bar{y}) - a(x_i - \bar{x}) - (b - \bar{y} + a \bar{x}) $$
I don't really understand what they do and why they do it.
To clarify:
$e_i = y_i - \hat{y}_i$, where $ \hat{y}_i$ is the regression function I believe it's called.
Some of the notation is not fully standard, and in particular, you seem not to be distinguishing between the (unobservable) errors $e_i$ and the (observable) residuals $\hat e_i$.
The usual notation runs like this: