The law varies state-by-state but the actual process is closer to the following:
(1) Company agrees to file for bankruptcy, usually because they feel it's in the owners' (shareholders) best interest to do so, in terms of recovering as much as possible from the "bad" company, vs. trying to keep the company running.
(2) Company's management comes up with a plan to distribute company's assets (cash, servers, IP, whatever) more or less "fairly" among the creditors. A big part of this is preventing management from giving a sweetheart deal on something worth a lot, to their friend/relative/whoever (e.g. selling the customer list for $1 to their friend's company), vs. making sure everyone who has a claim on said assets can get the most back as possible. A lot of arguments happen here, this is the main point of the rdio situation, whether the sale to Pandora is the best possible outcome for everyone.
(3) The company's assets get distributed as follows:
Employees -- most states give employees' unpaid wages preferential treatment in bankruptcy, under the same legal doctrine of why employees can't be held civilly liable (sued) doing something under control of their employer -- employers have a general duty to care for/protect their employees. (Note that neither the high priority bankruptcy payment, nor the liability protection, applies to contractors. Keep this in mind next time you think contractors are "overpaid" earning 2x as much as a W2, on top of the fact that they get no benefits and have to pay both sides of FICA)
Secured creditors -- any creditor whose claim is "secured" or "backed up" by some kind of asset, e.g. mortgage lenders.
Unsecured creditors -- 1099 contractors, lessors, all vendors, debenture (bond) holders, basically anyone who claims they're "owed" by the company.
And finally, lastly, equity holders (stockholders). They don't necessary have to get zero, but often do, because they're so far down the list of who gets paid (what investment bankers call "capital structure seniority" -- they're very junior).
So yes, it might be more or less ratable within each class, but there's no guarantee that "shareholders will get nothing", nor is it strictly proportional.
This is the garbage collection of capitalism and I find it fascinating.
To be less glib: when your company can't pay it's bills and can't get more investment you file for bankruptcy protection.