The easiest way to figure out the key of a song is by using its key signature. The number of sharps/flats in the key signature tell you the key of the song. A key signature with no sharps or flats is the key of C (or A minor).
How do you tell what key a song is in by ear?
Simply use the low E string and play from the open E note up the frets, one by one, until you're confident the note matches the root of the chord being played. Each chord plays for around 15 seconds. As long as you know the notes on the fretboard, you'll be able to identify the note you're playing.
What is the formula to finding the key signatures?
You remember key signatures by counting up or down 5 steps. Starting from C major (which has no sharps and no flats) count 5 steps forward to get sharp keys and count 5 steps backwards to get flat keys.