They know they will get paid first if the company declares bankruptcy. 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.
Sometimes companies prepare a reorganization plan that is negotiated and voted on by creditors and stockholders before they actually file for bankruptcy. This shortens and simplifies the process, saving the company money. For example, Resorts International and TWA used this method. You will get a prospectus and a ballot, and it's important to vote if you want to have any impact on the process. Under the Bankruptcy Code, two-thirds of the stockholders who vote must accept the plan before it can be implemented, and dissenters will have to go along with the majority.