The first group of debts that need to be paid are the expenses that come up as part of the bankruptcy. Otherwise, the groups managing the bankruptcy would have no motive to handle the process. This group includes the law firm managing the bankruptcy, the accountant who handles the company's final accounting postings, the auction service that sells off the company's property and any other service that was purchased to help wind down the company's affairs.
After the company handles its bankruptcy costs, it starts paying off its business creditors. The company first pays off its secured creditors. Secured creditors gave loans based on physical pieces of property. These are debts like the mortgage on company buildings, leases on company cars and loans for unpaid pieces of equipment. Secured creditors get their money back first, usually by taking back their property. If this isn't enough to pay off the debt, the secured creditors get first dibs on any remaining company money.
If the bankrupt company still has money left over, it starts paying its unsecured creditors. These debts not based on a physical piece of property. Unsecured debts could be credit card bills, unpaid insurance premiums and bank loans that weren't backed up by property. It also includes payments to bondholders. Unsecured creditors charge higher interest than secured creditors because of this higher bankruptcy risk. When the company declares bankruptcy, the unsecured creditors have no guaranteed payment. They need to wait until the secured creditors are paid off and hope there is money left over.
Shareholders are last in line during the bankruptcy process. This is one of the reasons why stocks are a riskier investment than bonds. When a company declares bankruptcy, its stock becomes worthless. The shareholders only get money after all other debts are paid. Since the company was in bad enough shape to declare bankruptcy, this isn't especially likely. Shareholders should only expect to get a fraction of their investment returned after bankruptcy and shouldn't be surprised to get nothing at all.