In Germany, a layoff is called Betriebsbedingte Kündigung (Translation: "Termination due to reasons of corporate setup". Means the job is gone with no replacement, instead of the person was fired to hire someone else.)
Sind mehrere vergleichbare Arbeitnehmer betroffen, muss der Arbeitgeber die Arbeitnehmer, denen er kündigen möchte, zudem nach bestimmten sozialen Kriterien auswählen (Sozialauswahl). Er muss dabei die Dauer der Betriebszugehörigkeit, das Lebensalter, eventuelle Unterhaltspflichten und eine eventuelle Schwerbehinderung des Arbeitnehmers ausreichend berücksichtigen. Auf Verlangen des Arbeitnehmers hat der Arbeitgeber dem Arbeitnehmer die Gründe anzugeben, die zu der getroffenen sozialen Auswahl geführt haben.
Translation (emphasis mine for the context of this question):
If several comparable employees are affected, the employer must also select the employees whom he wishes to dismiss according to certain social criteria (social selection). In doing so, he must take sufficient account of the employee's length of service, age, possible support obligations and any severe disability. At the employee's request, the employer must inform the employee of the reasons that led to the social selection made.
So how do you weight 10 years and 2 ex-wife's against 5 years, a wife and 2 kids? There is no single formula, but the formula has to be made available to all who got laid off. It's called the "Sozialplan".
If you have no "Sozialplan" or your Sozialplan will not hold up in court, the termination is invalid and the company will need to pay the employee as if they had been employed all the time since the layoff. As a court ruling on those cases can take years, that is a pretty hefty risk in the books and many companies rather pay a good severance (which is not legally required in Germany at all) to make sure nobody sues, than having that risk in their books.