Gold is a commodity, it does not pay dividends. The S&P 500 is a stock index, it does not pay dividends either.
The S&P represents the price of those 500 stocks, but doesn't include dividends.
The price of gold represents, well, the price of gold, but doesn't include dividends.
It's a valid comparison.
When you start comparing gold to stocks, you have to include TOTAL return of both, or it's just another shabby goldshill halftruth.