The structure of algebraic Baer $^*$-algebras