The case where the bid is higher or equal to the ask is known as a "crossed book". I most cases, this should never happen and if it does it would be as a result of a bug in the matching algorithm. If you place a buy/sell order with a price that is in excess of the best ask/bid respectively then that order will be matched against the opposite side. Under normal conditions what you suggested should be impossible.
If you want to know more about how order books work, I wrote an article specifically about how this works mechanically: https://www.machow.ski/posts/2021-07-18-introduction-to-limi...
Generally if a bid is introduced that is higher than the ask, the volume at the ask is matched, then the volume leftover at each ask level is matched in order of price (then time).
First case: you ask for 100 and then I bid 105 --> transaction clears at 100, the ask price
Second case: I bid 105 and then you ask for 100 --> transaction clears at 105, the bid price
This is because I implicitly think about bid offers as "I want to buy this for at most X dollars" and ask offers as "I want to sell this for at least X dollars".
I might be completely wrong though.