In secondary stock market trading a surplus of shares occurs when current owners are eager to sell in some volume and current buyers are reluctant to buy at current prices in that much volume. A shortage of shares occurs when current owners are eager to hold for more gain and current buyers are eager to purchase at current or rising prices in some volume. A combination of factors drives the surplus or shortage at any time because there are traders working on different time frames, different fundamental models, and some traders also employ technical analysis on one or more time frames.
Nasdaq(TM) BookViewer(TM) product provides a real time representation of the depth of book (product splash page):
https://data.nasdaq.com/BookViewer.aspx
User Guide (six pages):
https://data.nasdaq.com/pdf/Bookviewer3_UserGuide.pdf
If trade strategies are executed using automated systems (robots) then some of the order information may be processed too rapidly for a human being to follow in real time. Setting aside the problems of automated trading the mechanics of a Last Match or Last Sale are best understood in the context of aggregating the order book into a structured view model. These models look similar to the BookViewer image shown in the User Guide.
User Guide quotes:
Last Match (Price) — Reflects the execution price from the most recently matched orders on for the particular security on the Nasdaq stock market. Please note that trades matched on other venues are not included in the calculation.
Buy Orders and Sell Orders - BookViewer automatically displays up to the first 30 individual open visible buy and sell orders, that are available for instant matching. Buy orders are on the left, and sell orders are on the right. Orders are presorted according to execution priority (price and time) so orders higher on the list will be executed before orders lower on the list. Hidden orders are not displayed.
Shares — Reflects the number of shares per order, available for matching. The displayed amount may be less than the original number of shares entered if the order was partially executed or partially canceled.
Price (Buy/Ask) —Reflects the limit price for each order.
A keyword search for "aggressive limit order" shows a reference that makes the following statement:
The way of achieving full execution of an order is to use an aggressive limit order, meaning an order that has a higher price than the best prices at the other side of the market and walks up the limit order book. For a buy (sell) order, this means it has a price higher (lower) than the best ask (bid).
So in simplified general terms a transaction at the Last Match or Last Sale occurs when a market order is filled at the best prices available in the order book at the time when the trade executes; or occurs when an aggressive limit order is placed to hit bids or lift offers from the order book.