I don't feel any of the existing answers give a very concrete sense of why this is impossible. The basic reasons are similar for any traded good, but are easy to understand in the case of stocks thanks to market microstructure.
Start with a single company. Say you wanted to purchase every share they had outstanding and that was 1 million shares. If you were willing to pay whatever price is offered, as you stipulated in your question, this would be a market order, and would go to your broker and be placed on their order book as 'BUY, 1000000, MARKET.' For an actual transaction to occur, this order needs to be matched against the name number of shares worth of sell orders. Initially, these would not exist. The quote you see on an exchange ticker is the clearing price of the last transaction, which doesn't indicate that you can actually purchase anything at all for that price. You can only purchase what the current holders of the stock are offering.
Say every order book on that exchange cumulatively contained something like this, with an exchange quoted price of $10 per share:
SELL, 10, MARKET
SELL, 10, 10.01
SELL, 10, 10.02
And that's it (to keep the example very simple). Your buy order would first get matched against the market sell order and you'd get your first 10 shares for the quoted 10.00 per share. Your next 10 shares would be matched against the 10.01 limit sell, and the next 10 after that would be matched against the 10.02 limit sell, so you'd end up with 30 shares total for an average price of 10.01 per share. The exchange would now quote 10.02 as the market price, since that was the clearing price of the last transaction. You'd have to wait for someone else to sell, and they'd only sell for at least 10.02.
I think you can quickly imagine how this process compounds itself and your giant market buy order would only cause the price of the stock to exponentially increase until everyone with liquid shares sold them to you, with the smartest sellers noticing you have a ridiculous order and only offering you shares at a ridiculous price. There is nothing at all forcing them to offer you shares at the market price quoted on the exchange at the time you placed the order. You can see how you'd need quite a bit more, maybe impossibly more, than the current market cap of the company to actually buy the entire company.
In practice, to take a company private, you don't need to buy every share. You only need to deregister with the exchange. The rules for when you're allowed to do this vary by country. In the U.S., it is currently when you have fewer than 2,000 shareholders or the book value of your assets has not exceeded $10 million for the past three fiscal years. Of course, you'd need control of the board, which in most cases requires a controlling majority of voting shares. Instead of placing a market order for all shares outstanding, you'd privately negotiate the takeover with a sufficient number of shareholders to gain a controlling majority, and they'd understand you don't have an infinite bankroll and can only pay a reasonable price, and they aren't going to get a better price from any other buyer (assuming this is actually true, because they won't sell to you otherwise). Once you have a controlling majority, there is probably no good reason to try buying out everyone else until you're the only owner.