It’s not dissimilar to a commodity like Gold with some extra parameters like number of stocks.
Comes down to an open market and buy/sell demand.
There are other parts to your example such as depth of order book (orders on the buy sell side) which provide liquidity.
So if a low volume stock is at $4 and you offer to buy at $100 you would probably end up with 100/4=25 shares. If was more illiquid you could possibly move the price up depending on order quantity.