It reflects the evolutionary history of locomotion. Our wormy ancestors slithered on the sea floor, so undulated side-to-side. Fish inherited that movement, for which a vertical tail is best.
Their distant land mammal descendants evolved to run with limbs underneath: an unstable gait allowing rapid direction changes. To extend the stride, their spine flexes up-and-down. Marine mammals kept this movement, for which a horizontal tail is best.