In piano music, accidentals are notes that are not member of a scale. They are represented by flat, sharp or natural sign.
For example, in C major scale, notes are C-D-E-F-G-A-B so use of any sharp and flat is accidental.
However, in G major scale notes are G-A-B-C-D-E-F# so use of F sharp is not accidental.In this key set, use of F natural is accidental.
What does sharp, flat and natural sign mean?
The sharp sign in front of a note means that the note should be played half step higher to the right of that note.
The flat sign in front of a note means that the note should be played half step lower to the left of that note.

Usually both sharp and flat notes are black keys but there are two exceptions.

Since E and B notes don’t have black keys to the right of them,their E sharp (E#) and B sharp(B#) notes are white keys i.e F and C notes respectively.
Similarly, C and F notes don’t have black keys to the left of them so C flat (Cb) and F flat (Fb) notes are white keys i.e B and F note respectively.
The natural sign in front of a note means that sharp or flat is cancelled i.e we play the original pitch of the note.
In addition to flat,sharp and natural, there are double flat(bb) and double sharp (x) also.
As name implies double flat and double sharp lowers or raises note by two half-step (whole note) respectively.
For example, if we raise C note by half-step, we get C sharp(C#) and if we raise it by half-note one more time we get C double sharp (Cx) which is equivalent to D note.
Why do we use accidentals in music?
We use accidentals in music when we need to raise (sharp) or lower (flat) note temporarily. It makes musical composition more interesting because playing only within one key set can be boring. It adds different flavor to music because you are using notes outside of its key set.
How long does an accidental last?
When accidental is applied to the note, it lasts throughout the bar or measure.
It changes the pitch of note only within that bar. If you need to change the pitch of same note in the next bar, you have to apply accidentals again.
Since accidentals last till the end of the bar, to cancel it within the same bar another accidental should be applied.