"Why should I train them up when they might leave? Let somebody else train them, then I'll hire them afterwards." — Companies, in a prisoner's dilemma.
It's not a legal agreement but a gentleman's agreement. They may still freely leave for whatever reason, especially if they were not treated well or treated like a slave (ie employer broke the agreement), but if all goes well it's some assurance, or at least a communication so everyone is on the same page. And usually being on the same stage and starting out with such trust sets the stage nicely for a good, trust-based safe working relationship
The juniors you train today someone will poach, surely, as you'll poach someone else's, and it levels out.
Fast forward half a decade forward, and there's no juniors or mediors to onboard, no-one to have gemeric expertise that can be fine tuned to your product.