Involuntary Sep is different for officers vs. enlisted...
For an enlisted person, they don't really have to "seperate" you, when your enlistment expires, you're done. Whether or not they allow you to re-enlist depends on their needs. In recent memory the navy at least has required junior enlisted to actually apply and be screened for their first re-enlistment.
For officers, your commission is technically supposed to be harder to remove. They can obviously do it for misconduct or obvious unsuitability, and the modern military also has well established time-in-grade limits. The time-in-grade limit means that you can only serve X number of years of commissioned service based on your rank. Typically it is...
O1-03: 12 years
O4: 20 Years
O5: 24-28 Years
O6: 30 Years
Flag: > 30
The key point here is that you need to get promoted three times to reach O4 in order to serve 20 years and get a retirement (that's why O4's tend to be more outspoken...the boss can't cancel their pension). An O3 who fails to select for O4 will get the boot well prior to retirement. Enlisted have similar time-in-grade limits, and you normally need to make E5 to reach 20.
Normally the the military thins the officer ranks through voluntary attrition and time-in-grade limits. If they need more reduction, they usually do some sort of due-process where a board decides who to boot. It works exactly like a promotion board except the focus is on the bottom performers.
If you have less than six years service, you can (and probably will) get invol sep without any compensation. If you have 6+ years, they usually have to pay you some lump sum. If you have 18 years, they have to let you stay in to reach 20.
The best way to avoid this is to perform well and get promoted.