In music, an accidental is a sharp, flat, or natural that is not in the key signature. It affects the note directly to its right and stays in effect throughout the entire measure unless otherwise noted (haha, pun!).

The interesting thing about accidentals is which ones a composer chooses to use. For every note there are possible enharmonic spellings, i.e. a pitch can be written in different ways but ultimately sounds the same. For example, A# is the same as Bb, and E natural is the same as Fb. Therefore, when a composer alters a pitch, he or she must choose which note to write it as.

Traditionally, the note written dictates its resolution.* Sharps resolve up and flats resolve down; naturals resolve either way depending on whether their notes were originally sharped or flatted. In short, lowered notes resolve down and raised notes resolve up. So if a composer writes an A#, the implication is a movement towards B. However, if he writes Bb, which sounds the same to the ear, the implication is a movement towards A.

A good example of this is the case of German sixth chords used in a major key. Augmented sixth chords are predominant in nature, meaning they lead to the V chord. In order to avoid parallel motion, these chords generally progress to a second-inversion tonic chord followed by a dominant chord (i64 - V). Normally, in minor, Gr6 chords are spelled as scale degree 1, b3, #4, and b6. This works because in the tonic chord, 3 is still flat. However, in major, they are often spelled as 1, #2, #4, and b6. This is because b3 is no longer a common tone between Gr6 and i64. Writing b3 in the Gr6 chord implies movement towards 2, but there is no 2 in a tonic chord. Instead, the b3 moves up to natural 3, so writing #2 is more appropriate because it implies the upwards resolution.

If you're not familiar with some of the terms in the previous paragraph, some write-ups you might find helpful are Augmented 6th chord and Degrees of the Scale. Feel free to msg me with any questions as well.

*rp has kindly pointed out that the way I stated this is backwards: the resolution dictates the accidental. So if C# is going to C natural, the composer would write Db instead. I explained it the way I did because that's how theorists view it: a Db would imply movement towards C; a C#, movement towards D. Really it is not the accidental that decides where the chord is going, but the resolution that decides how the accidental was written. I hope that clarifies things.