As an example (this is fairly common) below is an excerpt from a Bagatelle in C minor by Beethoven (imslp link).
Notice the F and C (!!) are sharpened. Also notice As and Es are natural. So what's happening here is that Beethoven is going outside the key for a bit, probably using borrowed chords (I'm not as fast an analyzer as I should be).
Why does he use F♯ and C♯ instead of D♭ and G♭? I hand analysis over to MattPutnam (emphasis mine):
The first measure starts and ends with a G minor chord. The middle beat is acting as a secondary dominant to that G minor, which needs an F♯ (I would call it vii°/V). Similarly in the next bar, the middle beat is C♯° leading into D. Using a D♭ or G♭ would be a harmonic mess, not to mention the difficulty of reading G-G♭-G♮ and D-D♭-D♮.
Another good reason would be to make the piece easier to read and/or play. See this question: Temporarily Changing Keys - Which accidentals to use?