Let $\mathrm{P}(a,f(a))$ be a point on the graph of $f$. If you rotate that point $90$ degrees anticlockwise about the origin, then $\mathrm{P}$ is mapped to $\mathrm{P'}(-f(a),a)$ (there are a number of ways you can justify this, e.g. with matrices). If the graph of $f$ is invariant under a $90$ degree rotation, then it must be the case that $\mathrm{P'}$ also lies on the graph of $f$. Symbolically,
$$
f(-f(a))=a \, .\tag{*}\label{*}
$$
So the functions that are invariant under a $90$ degree rotation are precisely those that are solutions to $\eqref{*}$. I believe it is difficult, if not impossible, to find the general solution of this equation, so naturally we look for examples.
To make the problem easier for ourselves, suppose that $f$ is even or odd. If $f$ is even, then
$$
f(-f(a))=f(f(a))=a \implies f(a)=f^{-1}(a)\, .
$$
This means that any even function which is its own inverse satisfies $\eqref{*}$. Unfortunately, it is impossible for an even function to be its own inverse! Try figuring out why.
If $f$ is odd, then the equation boils down to $f(f(a))=-a$. This equation has been asked about on MSE before. Alex Jordan's answer mentions the function $g(x)=\frac{x}{|x|}-(-1)^{\lceil|x|\rceil}x$ which looks almost identical to the function you asked about in your question (the only difference is the behaviour of the function at the endpoints of each line segment):

This visual tells us something important. While the formulae describing the functions $f$ and $g$ look formidable, the graphs of the functions themselves are fairly simple-looking. It's easy to see that the above graph describes a function which satisfies your equation—the hard part is finding a concise formula that describes the function. Almost certainly the person who conceived of this example thought of the graph first, and only then found a clever way of describing the function using a formula.