The basic mechanism at work is that functions such like $|.|$ or $\min$ or $\max$ or $\Theta$ realize case decisions depending on their argument, together with the closedness of linear functions regarding linear combinations.
This can be exploited to combine these functions in such a way that a decision with at least $n$ cases can be realized to deal with the $n$ continous pieces of the piecewise linear function.
Update: I tried a different decomposition for $f$, but the "dynamic" approach that user Carsten Schultz posted turned out to be easier so far.
Here is a proof:
$$
\max(x, y) = \frac{x + y + |x - y|}{2}
$$
so we can write
$$
\begin{align}
\frac{a_{i+1}-a_i}{2} \left(\left|x - x_i\right| + x - x_i\right)
&=
(a_{i+1}-a_i)
\frac{\left|x - x_i - 0\right| + x - x_i + 0}{2} \\
&=
(a_{i+1}-a_i) \max(x-x_i,0)
\end{align}
$$
and therefore for $n$ pieces $f_i(x) = a_i x + b_i$ (we assume $n \ge 2$) and $n-1$ points of change $x_i$ (we assume $x_i < x_j$ for $i < j$) we get:
$$
f(x) := a_1 x + b_1 +
\sum_{i=1}^{n-1} (a_{i+1}-a_i) \max(x-x_i,0)
$$
We have three cases:
- left: $x \in (-\infty, x_1]$,
- inner: $x \in [x_i, x_{i+1}]$ and
- right: $x \in [x_{n-1}, \infty)$.
Inner case: For $x \in [x_j, x_{j+1}]$ we have $x \le x_{j+1} \le x_{i}$ for $i\ge j+1 \iff i > j$ and thus $x - x_i \le 0$, so the maximum terms for $i > j$ vanish and the equation above reduces to
$$
\begin{align}
f(x)
&=
a_1 x + b_1 +
\sum_{i=1}^j (a_{i+1}-a_i) (x-x_i) \\
&=
a_1 x + b_1
+ x \sum_{i=1}^j (a_{i+1}-a_i)
- \sum_{i=1}^j (a_{i+1}-a_i) x_i \\
&=
a_1 x + b_1
+ x \left(a_{j+1}-a_1\right)
- \sum_{i=1}^j (a_{i+1}-a_i) x_i \\
&=
a_{j+1} x + b_1
- \sum_{i=1}^j (a_{i+1}-a_i) x_i \\
\end{align}
$$
From the continuity requirement at the point of change $x_i$ we get
$$
a_{i+1} x_i + b_{i+1} = a_i x_i + b_i \iff
(a_{i+1} - a_i) x_i = b_i - b_{i+1}
$$
this gives
$$
\begin{align}
f(x)
&=
a_{j+1} x + b_1
- \sum_{i=1}^j (a_{i+1}-a_i) x_i \\
&=
a_{j+1} x + b_1
- \sum_{i=1}^j b_i - b_{i+1} \\
&=
a_{j+1} x + b_1
- (b_1 - b_{j+1}) \\
&= a_{j+1} x + b_{j+1}
\end{align}
$$
Right case: All $n-1$ maximum terms turn into $x - x_i$.
$$
\begin{align}
f(x)
&=
a_1 x + b_1 +
\sum_{i=1}^{n-1} (a_{i+1}-a_i) (x-x_i) \\
&=
a_n x + b_1
- \sum_{i=1}^{n-1} (a_{i+1}-a_i) x_i \\
&=
a_n x + b_1
- \sum_{i=1}^{n-1} b_i - b_{i+1} \\
&= a_n x + b_n
\end{align}
$$
Left case: All $n-1$ maximum terms vanish.
$$
f(x)
=
a_1 x + b_1
$$