It’s magnets! Seriously. There’s a layer of iron underneath the cue ball that gets nudged and pulled by a strong magnet inside the pool table so that it gets put on a completely different track from the rest of the balls in pool.
That track is the one that spits out the cue ball. The other balls in pool don’t have metal inside so they don’t react to the magnet at all and just get sent down to their regular station through the regular track.