As in the answer here (which you referred to yourself), the price of a stock is the price the stock was last traded at (until that price is updated because a new trade happens). A trade occurs if a bid and ask are matched.
The matching relies on a double ordering. The principle to remember here is "buy low, sell high". Also, remember that the ask is the minimum price a would-be seller is happy to sell, while the bid is the maximum price a would-be buyer is happy to buy. The buy and sell offers are ordered in the following way:
- Asks: lowest first, highest last, then in the order they were submitted.
- Bids: highest first, lowest last, then in the order in which they were submitted.
This is called price-time-priority matching (see here). Many exchanges use a variant of that procedure with potentially some extensions. An important exception is the NYSE, which has a pro-rate system on top (see here), but this is not relevant for your cases.
What happens when the bid is higher than the ask? For example, someone is ready to pay \$101 per share for 100 shares, and someone wants to sell 100 shares at \$100. What will be the new price?
The matching in this case depends on the order in which instructions were submitted and on the best price rule, which says that whoever submits last gets the best available price. One motivation for that rule is that agents shouldn't be punished for submitting limit orders. After all, if that principle were not in place, agents who act last could instead put in market orders (without a limit), where a buyer immidiately buys at the lowest ask, and a seller sells at the highest bid, which may result in a better deal for the agent last to act. If not in place, you wouldn't get many limit orders, if any at all. So this rule favors market liquidity.
Applying these rules, if the sell offer at \$100 was there first, the buyer, even though she is happy to pay \$101, will only pay 100. Conversely, if the buyer places the order first, the seller will sell for 101. So, depending on the order of submission, the resulting price will be either 100 or 101.
What if there are multiple matches at an instance? Let's say we have someone wanting to buy 100 shares at \$100 and someone wanting to sell 100 shares at $100. We also have someone wanting to buy 500 shares at \$110 and someone wanting to sell 500 shares \$110. What is the new price?
Again, the matching depends on the double ordering mentioned above and the best price principle. I won't go through all possible cases, but provide a few illustrations:
Bid = 500@\$110. The seller comes in with 100@\$100. The trade is executed at the best price (for the seller) at \$110. (if they submit in the reverse order, the trade will happen at a price of \$100)
Bid = 100@\$100, Ask = 500@\$110. There is no match. Ask = 100@\$100 comes in, "jumps the queue," and 100 shares are traded at \$100.
Bid = 100@\$100, Ask = 500@\$110. There is no match. A new bid comes in with 500@\$110. It jumps the bid queue and is matched with the ask, and the trade happens at a price of \$100.
Note that time-stamping these days is very accurate, to within one millisecond (see for example here). For any confusion to occur, orders would have the share the same time-stamp and precisely the same price.