The general rule is that the persons who take the least risk are paid first. Secured creditors, such as a bank lending money backed by a mortgage on real estate, typically bargained for taking less risk. Assets of the company usually back the credit that they extend. They know they should get paid very early on the list if the company declares bankruptcy. General creditors, such as suppliers of goods and services, and other lenders and bondholders, have a greater potential for recovering their losses than stockholders, because bonds represent the debt of the company and the company has agreed to pay bondholders interest and to return their principal.
Stockholders own the company, and take greater risk.
They could make more money if the company does well, but they could lose money if the company does poorly. The owners are last in line to be repaid if the company fails. Bankruptcy laws determine the order of payment. Most publicly-held companies will file under Chapter 11 rather than Chapter 7 because they can still run their business and better control the bankruptcy process. Chapter 11 provides a process for rehabilitating the company's faltering business.