I think the golden rule of a good mystery is the clues should always all be there by the time the detective works out the solution, so that the reader could hypothetically work it out too, though they probably won't, they'll probably go "OH! Of course!" and it'll feel really satisfying.
In bad mystery fiction, the solution comes out of nowhere because key information wasn't revealed to the audience.
So as an example, in a murder mystery, the killer should always be a character the audience has met or heard mentioned in the story, it can't be some random character who suddenly turns up out of nowhere, and the murder weapon must be something that's been mentioned or shown.
As @Aeneas1 said, use plenty of red herrings, so bury the clues amongst a bunch of misleading leads that seem plausible but aren't the right ones so that they are there in plain sight, but kind of hidden until everything clicks into place. The most obvious solution should never be the correct one in a mystery.
I'd definitely recommend reading some classic mystery novels like Sherlock Holmes or some Agatha Christie novels, and definitely watch the movie "Knives Out" which is an absolutely excellent love letter to the genre.