Most stocks are traded on exchanges, which are places where buyers and sellers meet and decide on a price. Some exchanges are physical locations where transactions are carried out on a trading floor. You've probably seen pictures of a trading floor, in which traders are wildly throwing their arms up, waving, yelling, and signaling to each other. The other type of exchange is virtual, composed of a network of computers where trades are made electronically.
The purpose of a stock market is to facilitate the exchange of securities between buyers and sellers, reducing the risks of investing. Just imagine how difficult it would be to sell shares if you had to call around the neighborhood trying to find a buyer. Really, a stock market is nothing more than a super-sophisticated farmers' market linking buyers and sellers.
Before we go on, we should distinguish between the primary market and the secondary market. The primary market is where securities are created (by means of an IPO) while, in the secondary market, investors trade previously-issued securities without the involvement of the issuing-companies. The secondary market is what people are referring to when they talk about the stock market. It is important to understand that the trading of a company's stock does not directly involve that company.