AI was trained on a massive chunk of the internet. So when you ask it a question, it looks at your words and asks itself: based on everything I've seen, what word is most likely to come next? At the end of the day, it's really just a prediction machine.
The reason it's so good is that it has seen billions of examples of sentences and data, so its guesses are usually right.
This is also why good instructions matter so much. AI takes what you give it and predicts from there. When you give it higher quality inputs, meaning more specific details and clearer instructions on how you want the output, you can expect the answers to be way better. That's basically the whole game.
Two things stay true because it's a prediction engine. First, it will always be a little bit random. Second, it has a hard time with true zero-to-one creativity. Everything it makes is based on what already exists, which is why people say AI "has no soul."