To avoid buying or selling a stock at a price higher or lower than you wanted, you need to place a limit order rather than a market order. A limit order is an order to buy or sell a security at a specific price. A buy limit order can only be executed at the limit price or lower, and a sell limit order can only be executed at the limit price or higher. When you place a market order, you can't control the price at which your order will be filled.
For example, if you want to buy the stock of a "hot" IPO that was initially offered at $9, but don't want to end up paying more than $20 for the stock, you can place a limit order to buy the stock at any price up to $20. By entering a limit order rather than a market order, you will not be caught buying the stock at $90 and then suffering immediate losses if the stock drops later in the day or the weeks ahead.
Remember that your limit order may never be executed because the market price may quickly surpass your limit before your order can be filled. But by using a limit order you also protect yourself from buying the stock at too high a price. Some firms may charge you more for executing a limit order than a market order.