ChatGPT is in the Large Language Models (LLM) category. The most (in)famous GPT model is probably GPT-3, because since then, researchers realized that LLMs mostly follow a predictable scaling law, thus the more data and the bigger model, the better. It is accurate to say that ChatGPT was trained with Stack Overflow data, but it should be all Stack Overflow instead of just most upvoted answers/comments.
The Wikipedia page of GPT-3 and their paper mentions that GPT-3 was trained on multiple datasets, and one of which is the Common Crawl, which basically crawls everything on the Internet. Some data pre-processing was done before training, but the authors did not mention removing the comments, so we can say that it is all Stack Overflow data.
If we look at the Common Crawl data in Sep 2022, there is indeed the domain
com.stackoverflow in their list. Thus, while ChatGPT was trained on Stack Overflow data, it is trained on all Stack Overflow data instead of just most upvoted answers.
However, if you think ChatGPT's code output is of high quality, think again, because Stack Overflow temporarily bans ChatGPT
because the average rate of getting correct answers from ChatGPT is too low, the posting of answers created by ChatGPT is substantially harmful to the site and to users who are asking and looking for correct answers (quoted from the link).
Here is the justification of them:
The primary problem is that while the answers which ChatGPT produces have a high rate of being incorrect, they typically look like they might be good and the answers are very easy to produce. There are also many people trying out ChatGPT to create answers, without the expertise or willingness to verify that the answer is correct prior to posting. Because such answers are so easy to produce, a large number of people are posting a lot of answers. The volume of these answers (thousands) and the fact that the answers often require a detailed read by someone with at least some subject matter expertise in order to determine that the answer is actually bad has effectively swamped our volunteer-based quality curation infrastructure.
EDIT: a comment below also confirmed that GPT-2, the predecessor of GPT-3 and ChatGPT, was trained with Stack Overflow data.