The stock market is driven by supply and demand. The number of shares of stock dictates the supply and the number of shares that investors want to buy dictates the demand. It's important to understand the for every share that is purchased, there is someone on the other end selling that share (or vice versa). The stock market is really just a big, automated superstore where everyone goes to buy and sell their stock. The main players in the stock market are the exchanges. Exchanges are where the sellers are matched with buyers to both facilitate trading and to help set the price of the shares. The primary exchanges are the Nasdaq, the New York Stock Exchange (NYSE), all of the ECNs (electronic communication networks) and a few other regional exchanges like the American Stock Exchange and the Pacific Stock Exchange. Years ago, all of the trading was done through the traditional exchanges (like the NYSE, American and Pacific Exchanges) but now almost all of the trading is done through the Nasdaq, which uses ECNs and thousands of other firms with access to the Nasdaq to facilitate trading.
Here's an example of one of the many ways that the stock market works:
You open an account with E*Trade. You send E*Trade a check for $1,000. E*Trade deposits the check into a trading account that is listed under your name. You log onto E*Trade and place an order to buy 100 shares of a stock in Company A, which is currently trading at $5. E*Trade uses it's network to tell the Nasdaq and all of it's related networks that there is demand for 100 shares of Company A's stock. The Nasdaq finds someone who is willing to sell 100 shares of Company A and, instantaneously, they execute the trading of stock between you and the person selling the shares. The trade information is sent to a clearinghouse where the information is processed and the shares will now be registered to you. Basically, the clearinghouse will designate 100 shares of Company A to E*Trade and E*Trade will designate those 100 shares as yours. The actual stock certificates are typically held "in street name" and never really need to exchange hands (although you could request that the stock certificates be transferred to your name). In a nutshell, that's how the stock market works. The stock market is really just like any other marketplace - it facilitates the exchange of goods between interested parties and works to reduce distribution costs and set prices.