This might be TMI for you but....
A company may decide to "go public" with an initial public offering (IPO) of its shares if they want to raise significant money or make the shares more liquid. (Private company shares have a limited market for various reasons and can be hard to sell which depresses their value.)
The first step in an IPO is to consult with investment banks (usually more than one are involved). They determine if there is a market for your shares and what the potential price would be.
Say the banks determine that you can sell one million shares at $1.10 each. They then agree to buy those shares at $1 each and pay that risk-free money directly to the company. When the shares open for sale to the public, the investment banks take the risk that they will sell for $1.10 or more, as that is how they make their money. If the price soars while the banks still have not sold all of their shares, they make more. If it plunges, they don't make as much. Having a stock price soar immediately is not a good thing for a company as it means they "left money on the table" as the company was undervalued and the share price paid to them should have been higher. For example, if the share price was $2 at the end of the first day, they should have received more like $1.90 from the banks and so they failed to capture a lot of money. It is good for the banks as they made more money but bad in the sense that no one wants to habitually underprice a stock as then companies will not want to use you.
The shares then trade hands at whatever price people agree to sell and buy for. The company can then use that share price to issue options to employees (who then make money if the price goes up and nothing if it goes down) or to sell shares to outsiders to raise more money.
If a company "goes bankrupt" (which can mean many things), it may be for a variety of reasons. Sometimes, like SunEd, they still have a positive asset/liability ratio and still have some value. In that case, shares will stop trading while they are in bankruptcy protection, but eventually when that process finishes, they can start trading again. So if you bought a share at $10 prior to bankruptcy, you may be able to sell it now for $10 or $1 or,like SunEd, $0.37, down from $38 I believe. Or maybe more as sometimes companies emerge much stronger after the bankruptcy process is over.