A quaternion is an expression of the form
$$ x\bold i+y\bold j+z\bold k+w $$
where $\bold i$, $\bold j$ and $\bold k$ are basis quaternions, who’s algebraic behavior is defined by
$$ \bold i^2=\bold j^2=\bold k^2=\bold i\bold j\bold k=-1 %comment $$
Here is the multiplication table for the elements. (left multiplied by the top)
$$ \newcommand{\qi}{\color{#db3e1d} \bold i} \newcommand{\qj}{\color{#9af348} \bold j} \newcommand{\qk}{\color{#3a7af8} \bold k}
\begin{array}{r|c|c|c|} &\qi &\qj &\qk\\ \hline \qi &-1 &\qk &-\qj\\\hline \qj &-\qk &-1 &\qi \\\hline \qk &\qj &-\qi &-1 \\\hline \end{array} $$
We can use the unit quaternion as a special encoding of the axis-angle representation of rotations. This encoding happens to be computationally very efficient for composing rotations. Under this interpretation, a unit quaternion that rotates by $\theta$ around an axis $\bold a$ is given by:
$$ \bold q=\cos(\theta/2)+\sin(\theta/2)(\bold a_x\bold i+\bold a_y \bold j+\bold a_z\bold k) $$
In order for us to rotate a vector $\bold v$ using a unit quaternion $\bold q$, we interpret $\bold v$ as a quaternion with a real component ($w$) of 0. Then, we can calculate the rotated vector $\bold v'$ using the quaternion $\bold q$ and its conjugate $\bold q^*$ or the more expensive inverse $\bold q^{-1}$
$$ \bold q^{-1}=\frac{\bold q}{||\bold q||^2} $$
$$ \bold v'=\bold q\bold v\bold q^{-1} $$
The composite rotation $\bold c$ of rotating by $\bold q$ then $\bold p$ is given by multiplying them together. Note that $\bold p\bold q$ is not the same as $\bold q\bold p$, as quaternions are non-commutative.
This composition works because, imagine the consecutive rotations:
$$ \begin{align*} \bold p(\bold q\bold v\bold q^{-1})\bold p^{-1}=\\ (\bold p\bold q)\bold v(\bold p\bold q)^{-1}
\end{align*} $$
Recovering the axis $\bold a$ and angle $\theta$ from a quaternion $\bold q$
$$ \begin{align*} \text{let}\space\bold v &= (\bold q_x,\bold q_y, \bold q_z)\\ \bold a&=\hat{\bold v}\\ \theta &= 2\cdot\text{atan2}(||\bold v||, \bold q_w)
\end{align*} $$
The rotations $\bold q$ and $-\bold q$ represent the same final orientation, but not the same relative rotation distance. Just like in 2D, $-90\degree$ is the same orientation as $270\degree$, but not the same relative rotation
Interpreting the bases: