The economy and the stock market are closely related. Many people examine the stock market to find out how the economy is doing. It's long been known that if the stock market is in a period of decline, the economy is sure to follow. However there is little evidence that the stock market causes the economy to rise or fall. The stock market does not directly affect the economy. It is simply a mirror of people's generally correct beliefs about what is about to happen in the economy. The best way to understand this is to realize that a stock market index the Dow Jones Industrial Average (DJI) is simply a price. Because the value of index is a price, it only has two determinants: supply and demand.
Supply
Any first year college textbook in Economics states that for most goods if the supply increases in the short run then the price of the good should decline. For example, if the car companies suddenly doubled their supply of cars then we would expect the price of cars to fall.
If we thought that changes in the supply of stocks are the main cause of stock market rises and declines then, according to this rule, when a company issues new stock we would expect the price of stock to decline. If stock prices are largely determined by the supply of stocks and the market declines prior to an economic decline, we should see a flood of new stock issues before a recession. This does not happen in practice, as new stock issues tend to occur as the economy enters a growth period. This is because the money made from a stock issue is used to increase the output of the company, which causes economic growth to rise.
Demand
It appears that if we want to understand why the economy tends to move in the same direction as the stock market, we'll have to consider the demand for stocks. To do this, we'll need to understand what motivates an investors decision to buy or sell shares. Many investors such as Warren Buffett evaluate their stock portfolios on their inherent value. The inherent value is the total expected earnings of the company over a time period, discounted by the fact that a dollar today is not worth as much as a dollar tomorrow. If investors believe that a recession is coming, then they will believe that company earnings will be less in the future (since that typically takes place in a recession) which will decrease the inherent value of the stock. When the inherent value of the stock is far below its current price, investors will sell the stock, driving the price of the stock down. If investors believe a boom is coming, they will increase their estimates of the inherent value because future earnings should be higher than they previously expected. Often this will lead to the inherent value being far higher than the current price of the stock, so investors buy the stock. This leads the price of the stock to rise.
The belief that the stock market drives the economy is due to an error in logic. Generally we think that if A came before B that A caused B. Philosophers refer to this as the post hoc, propter hoc fallacy. In this case, the expectation of a decline in the economy causes the stock market to decline today. Or in logical terms, A came before B, because the expectation of B caused A. It's also important to realize that it's not the expectation of future economic changes that is causing changes in stock prices. It's the fact that people are acting on these expectations. If investors bought and sold stocks based on astrological factors or Barry Bonds' current homerun total then these would be causing the price of stocks to change. In a situation like that, it would seem that the stars are causing the price of stocks to change; the economy would have nothing to do with it.
It is because a large number of investors act on this inherent value principle that the economy tends to follow the stock market. Investors are constantly watching macroeconomic variables to try and determine when the next downturn in the economy will happen. Investors are often right when they predict the future growth rate of the economy. As a result, they often sell off their shares before the economy goes into a decline making it look like the stock market is causing a recession. In reality the causality runs the other way because the two things that causes price to change are changes in supply or changes in demand.