If a company has lay-offs and needs to reduce staff, and the staff are all equally good employees, then the employer is forced to chose to fire 'at random.'
I believe you're confusing firing someone because they have a certain characteristic that you can't use as your reason to fire them, e.g., the elderly, women, racial minorities. If you "randomly" fire one of them because of that trait, then that is illegal. If they have that trait and you fire them, you need to make sure there's no history of singling out people with that trait.
Businesses tend to keep records of job performance and evaluations to show that they didn't target someone with that trait because even if it wasn't a firing based on prejudiced, you can be sued if it looks like it. And even if you would win that suit, no company wants to deal with that litigation. So, then tend not to fire truly "at will" to avoid litigation.
But, they can fire at will.
Local state laws nothwithstanding.