It can get a little more complicated than that though -- you might be trying to sell 1000 shares and the highest bid might only be for a quantity of 500 so the "market" price for your first 500 shares will be different than the next 500 (unless there are other bids at that same price, which there often are but there's no guarantee on the volume you'll be able to sell at that price).
And that's why they call it a stock exchange... it's just a bunch of people (and companies) making bids and offers to "exchange" stocks at difference prices. When a buyer and a sell agree on a price, you have a transaction (trade). Your offer to sell at market price is just an agreement between you and someone else willing to buy at that price.
You could also do what they do. Instead of buying immediately at market price, you could enter a lower price and create a standing order to buy at that price. However, if the price goes up, you might miss out on buying the stock at all.
Similarly for selling. You can enter a higher price, but it won't sell if the market price doesn't go up.
Conceptually this isn't so different from what a grocery store does. They offer something for sale and wait for a buyer willing to pay that price. However, margins are much lower and prices change much quicker for stocks.
(Note: I'm not recommending messing with any of this.)
Buying at market price simply means you immediately buy from the seller offering the lowest price.
It's very rare for an order book to be empty for a particular stock, but you typically do see the bid/ask spread increase as a stock loses popularity.
"market participants" which provide liquidity on an exchange.
Regulation NMS