For the Roman Republic, the answer is that the soldiers did not get to pick their own legion. Polybius, writing in the second century B.C., has given us a quite detailed description of how the soldiers were divided among the legions.
Usually four legions would be raised, but in times of need that number could be higher. First the tribunes (officers) are assigned to the legions to be raised by the popular assembly or the consuls. The tribunes would then select a tribe (a subdivision of citizens for legal and electoral purposes) by lottery. With this background set, I'll let Polybius describe how the soldiers got to their legion:
From each tribe they first of all select four lads of more or less the same age and physique. When these are brought forward the officers of the first legion have first choice, those of the second choice, those of the third third, and those of the fourth last. Another batch of four is now brought forward, and this time the officers of the second legion have first choice and so on, those of the first choosing last. A third batch having been brought forward the tribunes of the third legion choose first, and those of the second last. By thus continuing to give each legion first choice in turn, each gets men of the same standard. (6.20.3-7)