The number of shares of any given stock that are purchased on any particular day must equal the number of shares that are sold. Whenever there is a temporary imbalance between intended buying and selling, stock prices move upward or downward in order to re-establish an equilibrium between buyers and sellers. (Indeed, this is the sole role of marketplace prices.) Specifically, if there are temporarily more buyers for a stock than sellers at a given price, that price will necessarily rise to the point that enough potential buyers drop out of the bidding and enough existing owners of the stock, who weren't initially willing to sell, decide to sell at the higher price—until finally the number of shares purchasers want to buy is again equal to the number that sellers wish to sell. Similarly, if there are more sellers than buyers initially, then a stock's price will drop to the point that enough buyers join the bidding and enough sellers give up their efforts to sell because of the new, lower price.
Starting from an equilibrium point (where buying equals selling) for a given company's stock, what might cause a temporary imbalance between intended buying and selling? Basically, imbalances are caused by shifts in investors' level of optimism or pessimism regarding the outlook for the company and its stock. Similarly, in the case of the overall stock market, if most investors become more optimistic or more pessimistic, there will be an imbalance between overall intended buying and selling—necessitating a rise or fall in stock prices.
As you can see, when investors become more optimistic, stock prices rise to the point necessary to rebalance intended buying and selling, and when investors become more pessimistic, prices fall. In short, it can be useful to think of stock prices, either for individual stocks or for stocks in general, as a sort of thermometer that measures investor sentiment.
Stock prices change when there is a change in the amount of investor optimism or pessimism, and over the short run the exact level of optimism or pessimism isn't as important as the direction of change in sentiment. Prices rise whether optimism increases from a very low level to a slightly less low level or whether optimism increases from a high level to an even higher level. Over the longer term, however, the absolute level of optimism or pessimism becomes important in determining the degree to which sentiment changes can continue. For example, if optimism is already quite high, although it could rise somewhat further (as the few remaining pessimistic investors change their views and become optimistic), it will be difficult for optimism to rise much further. Put differently, when virtually every investor is already optimistic, who is left to turn optimistic? Thus, since it is the increase in the amount of investor optimism that causes stock prices to rise, once nearly everyone is optimistic, there isn't much room left for stock prices to rise further.
I like to think of the process of investor sentiment becoming more positive as "burning" pessimism—much like automobiles burn gasoline. In autos, when gasoline is burned it changes from one form (gas) to another (oxides of carbon and nitrogen, heat and kinetic energy), thus powering the vehicles. When cars run out of gas, they stop. In the case of stocks, the "combustion" of pessimism changes its form (to optimism), and this change powers stock prices higher. When we run out of pessimism to convert to optimism, stock prices stop rising. In short, pessimism is the fuel of bull markets. It makes sense that if we want to take a long drive, we should have a full tank of fuel. Similarly, for stock prices to rise a great deal, a full tank of pessimism will be required.
In sum, if you tend to think like many other investors and if you are feeling pessimistic, this is a good situation—it means that there is already a good deal of pessimism available to power a long bull market. When pessimism is plentiful, all that's necessary to start the process is an appropriate spark.