The quaternionic spectral theorem has already been considered in the literature, see e.g. [22], [31], [32], however, except for the finite dimensional case in which the notion of spectrum is associated to an eigenvalue problem, see [21], it is not specified which notion of spectrum underlies the theorem. In this paper we prove the quaternionic spectral theorem for unitary operators using the $S$-spectrum. In the case of quaternionic matrices, the $S$-spectrum coincides with the right-spectrum and so our result recovers the well known theorem for matrices. The notion of $S$-spectrum is relatively new, see [17], and has been used for quaternionic linear operators, as well as for $n$-tuples of not necessarily commuting operators, to define and study a noncommutative versions of the Riesz-Dunford functional calculus. The main tools to prove the spectral theorem for unitary operators are the quaternionic version of Herglotz's theorem, which relies on the new notion of $q$-positive measure, and quaternionic spectral measures, which are related to the quaternionic Riesz projectors defined by means of the $S$-resolvent operator and the $S$-spectrum. The results in this paper restore the analogy with the complex case in which the classical notion of spectrum appears in the Riesz-Dunford functional calculus as well as in the spectral theorem.