If you buy shares in a company, it doesn't necessarily mean you're buying it from another shareholder who wants to sell their stock. There are two main markets where securities are transacted: the primary market and the secondary market.

When stocks are first issued and sold by companies to the public, this is called an initial public offering, or IPO. This initial or primary offering is usually underwritten by an investment bank that will take possession of the securities and distribute them to various investors. This is the primary market. Investors participating in the primary market are thus buying stock directly from the issuing company.

Prices on the primary market tend to be set prior to the IPO, so the investor knows how much they will pay in order to invest in shares of that company's stock. However, this market is usually dominated by sophisticated and experienced investors, such as banks, pension funds, institutional investors or hedge funds.

The Secondary Market = The Stock Market

The secondary market is where investors buy and sell shares they already own and is more commonly referred to as the stock market. Any transactions on the secondary market occur between investors, and the proceeds of each sale go to the selling investor, not to the company that issued the stock or to the underwriting bank. Prices on the secondary market fluctuate and may be determined by basic forces of supply and demand. Therefore, unless you are an investor participating in an IPO, you are purchasing securities from another shareholder on the secondary market. 

A shareholder is considered to be any entity that has legal ownership of a company's shares. Having legal ownership means being recorded as the shares' owner by the company: When you buy a stock from another investor, three days after the transaction has occurred your name will appear in the company's record books, and you will be deemed the holder of record. The investor from whom you purchased the shares will at the same time be removed from the records.

Regardless of whether the investor selling you the stock is an individual, a financial institution or the company itself, it is considered to be a shareholder because it possesses legal ownership of the stock. The seller of a stock is forfeiting all associated rights to the shares, such as any dividends, distributions or further capital gains (or losses) from the shares they have sold.