The primary function of an exchange is to provide liquidity (the ease with which securities can be bought and sold without wide price fluctuations); in other words, to give sellers a place to «liquidate» their share holdings.
Stocks first become available on an exchange after a company conducts its initial public offering (IPO). In an IPO, a company sells shares to an initial set of public shareholders (a.k.a. the primary market). After the IPO «floats» shares into the hands of public shareholders, these shares can be sold and purchased on an exchange (a.k.a. the secondary market).
The exchange tracks the flow of orders for each stock, and this flow of supply and demand sets the price of the stock. Depending on the type of brokerage account you have, you may be able to view this flow of price action. For example, if you see that the «bid price» on a stock is $40, this means somebody is telling the exchange that he or she is willing to buy the stock for $40. At the same time you might see that the «ask price» is $41, which means somebody else is willing to sell the stock for $41. The difference between the two is the bid-ask spread.