The primary answer to your question is that although pitch defines the basic frequency of the note, there is—at least in common-practice tonal music, and many other styles too—an entire other trait called function. A C# and a Db are the same pitch (at least on the piano, these will often have slightly different tunings when played by unfretted string instruments or sung by vocalists etc.), but the C# is implying a need to resolve up to D while Db implies a need to resolve down to C. Not every composer or notater is as careful to maintain this distinction, but it's definitely the default.
As others have pointed out, different keys have different expectations for the diatonic pitches. One would expect to see C# in, say, A Major, but Db in Ab Major. In the rare case that a Db happens in A Major, it will be there specifically to imply a chromatic resolution to the (otherwise foreign to the key) C natural.
One of the great beauties of this particular notation system is that composers can indicate more than simply which buttons to press, or fingers to put down or notes to sing. Composers and arrangers can also show specific function of each and every note just what enharmonic choices they make. One sweet side effect of this is that a composer can imply one kind of movement or resolution, but then frustrate the tendency for dramatic effect while still easily informing the performer about how to interpret the notes.