I placed a buy market order during trade hours. The broker executed the order at a horrible price far outside the spread. The security's spread was wide, but I expected the order to be executed at least at a price below the ask price. How is this possible?
A market buy order means "buy at whatever the market is currently asking for" which is the ask.
It's possible you got front-run (someone else took the ask before you could) or the ask that you saw was filled before your market order was placed, but market orders are meant to guarantee execution, not to and lock in a price - that's what limit orders are for.
In other words, if the current ask was $6.50 and you wanted to try and get it for $6.45, you'd put in a limit order for $6.45 and hope that some buyer is willing to take it. Or, if you are willing to pay the ask but don't want to pay any more than that, then place a limit at the ask.
If the stock (or whatever) is illiquid, some High Frequency Trader may have seen your $6.50 order, realized that the next ask was $7.50, filled the $6.50 offer and sold to you for $7.50. That's a horrible deal for you but is plausible in an illiquid market.
In addition to Stanley’s answer, the amount at the shown ask price could be not deep enough for your order, ending in you buying through the order book upwards.
For example, someone offers 2 at 54$, and you want to buy 10 ‘at market’ - you will get those two plus the next eight at whatever price they are offered, maybe 55, 58, 67, whatever; giving you an average price anywhere above 54.