Doesn't that mean when you purchase index funds, you are significantly overpaying for some stocks?
I think "significantly" is a bit of an overstatement (it's not like stocks double when their inclusion is announced), and the vast majority of index fund buyers are not trying to "time the market" but are investing long term, so the future growth would far outweigh the slightly larger cost basis. In addition, since these indexes are market cap weighted, new entries make up a very small portion (e.g. much less than 1/500 of the S&P 500) of the index, so the initial bump in price has a very minor on the value of the index.
Shouldn't you purchase some stocks that's just outside the SP500 so that when they're added you can make a gain?
You can try - and this is called "Index Front Running". You can do some research and see how effective the strategy is. My research has shown that Index Trackers have largely caught up with this strategy and try to avoid it by changing exactly when they buy, even at the expense of some temporary tracking error.
Note that not all stocks "just outside the index" get added, so you will largely be guessing on what stocks will actually be included. You will be right on some and wrong on others.
I'm not saying it's a bad strategy, but since it is a known effect, much of it will already be "priced in", and there's less opportunity to capitalize on it.