In general, you use the notation that most clearly reveals the function or 'meaning' of the note to which it applies.
First, that means not using the wrong enharmonic form of a note--for instance, in the key of D major, which has 2 sharps, F# and C#, you should not normally use Db. That is the point that stratocarter is addressing. (Though it should be noted that his comment shouldn't be interpreted to mean that flats never appear in a sharp key or vice versa; it happens regularly, especially in minor keys.)
Second, it often means using the chromatic form appropriate to direction in which a non-chord tone proceeds, as in the common case where a chromatic scale uses sharps ascending but flats descending.
Third, it means using the enharmonic form appropriate to the chord. That is what daddyjb is talking about. For example, let's take the diminished seventh chord in C: B-D-F-Ab. Although the Ab sounds the same as G#, one should choose Ab here because we want to indicate that the B is the root; since chords are normally built in thirds, the diminished 7th with G# would be G#-B-D-F. In other words, G# would be the root. (It would also suggest the key of A, not C.)
Applying that to the example that kicked this discussion off, Gb Major would have the diminished 7th F-Ab-Cb-Ebb. Note that this is enharmonically the same as the chord we just examined! But spelling in thirds requires that we start with the F--and also necessitates the double flat.
By the way, technically the diminished 7th chord is not 'in' any *major* key; it's only found naturally in minor keys. (To illustrate this point, recall that we had to use an accidental to spell this chord in the key of C!) But borrowing from the parallel minor hasn't been unusual for more than two centuries now, so that's a nitpick for practical purposes.