How can the stock market keep growing indefinitely? It sounds like a dumb question, but there is only so much money in the world. Assets can grow, but money can only be printed. So if there is 400 trillion dollars worth of money in the world, and the stock market beats inflation by about 7% each year, eventually it will be projected to grow to be more than 400 trillion dollars, which should be impossible.
As mentioned in the other answer, the amount of money does not have to correspond to the total value of all assets in the economy. However, there is some correspondence that is not mentioned in the other answer so I will focus on that.
First, there should always be enough money in the economy so people can carry all the transactions they want. If that is not true the money market would not be in equilibrium. This can be formalized using the equation of exchange (see Mankiw, Macroeconomics 7th ed pp 86):
$$MV=P_TT$$
where $M$ is the stock of money, $V$ is the velocity of money (how many times is on average one bill used), $P_T$ are the prices and $T$ is the amount of transactions (which for the purposes of this answer can include asset transactions as well although normally this sort of model would be used only for goods and services).
We can learn several things from this formalization.
First, as the previous answer pointed out amount of money $M$ does not need to be equal to the value of assets, since the equation is $MV=P_TT$ not $M=P_AA$ (where $A$ would be assets). So the velocity of money matters as well. If you buy from me 1 stock of Apple for ${\\\$}100$ I can use the same ${\\\$}100$ dollar bill to buy other assets - so even though there is only single ${\\\$}100$ dollar bill multiple things can be purchased with it if it circulates fast enough. Moreover, because not all assets $(A)$ are always transacted $(T)$ there does not need to be correspondence between value of all assets and money as well.
Second the formula $MV=P_TT$ can be rearranged by decomposing the prices into aggregate price level $P$ and relative prices $P_r$ of the 'things' that are being sold on the market where the following equality would hold $P_TT=P P_r Q$  where $P_r Q$ would be the real value (i.e. inflation adjusted value) of $Q$'s  that are being sold (which we could also be in principle some asset). Lets also denote the real value of things being sold as $T_r$. So now the model would look like:
$$MV = PT_r$$
The equation above also shows us another thing. Even when $M$ and $V$ are fixed the real value of transactions $T_r$ can grow because prices adjust.
Ultimately what matters are real returns not nominal returns. When someone says that stock market returns beat inflation by $7\%$ that means that they real value increased by $7\%$ not that their nominal value is by $7%$ higher. For example, imagine that economy is experiencing deflation (negative inflation of $10\%$ and stock increases in value in real terms by $7\%$ per year. In such such situation if you invest ${\\\$}100$ dollars for one year in nominal terms at the end of next year your stock will be worth only ${\\\$}97$. However, real value of your asset (correcting for inflation or in this case deflation) is ${\\\$}107$ because deflation means everything in economy is getting cheaper and so you can purchase much more with ${\\\$}97$ than with ${\\\$}100$ year before.
The real value of asset  such as stock depends, very broadly speaking, on peoples' valuation of the underlaying companies. Constant improvement in technology and productivity on average leads to companies being more valuable on average over long time periods (of course, technological progress destroys some companies but it also allows other to flourish or completely new industries to rise up and we are talking about whole market here). Consequently, in real terms the only limit for how much stock market can grow are technological &amp; resource constraints avaiable to humanity (with emphasis on the technology).
In addition, it is worth noting there is no limit on how many money there can be in the world. Nowadays most of money is not even printed it is just created at a keystroke virtually. In principle if central banks would be willing to they could always find a way to expand money supply to any size they desire, so in theory $M$ can be any non-negative real number you can think of.
Hence in conclusion, the premise in your question ignores the velocity of money and the fact that not all assets are always transacted so there does not need to be enough money to purchase them all. And secondly even if velocity of money and and money supply is fixed the prices can still adjust in a way that allows for the market to grow in real terms.