The key of a piece is determined the scale used by that piece. Although the key may change during the song, there is usually a global key which the song starts and finishes with.
For example, even though there are multiply keys in the song Live Like There’s No Tomorrow (Selena Gomez), it starts and finishes on G major, so the global key is G major.
Watch out for inverted chords, such that the chorus and bass note may not be the scale note. For example, California Gurls (Katy Perry) is in F major and not A major.
Watch out for the anacrusis (“ana” Greek for “up towards”, and “crusis” meaning “to strike”), also known as an upbeat. So even though a piece may start with a particular note, it may have nothing to do with the key the song is in. For example, in This Boy That Girl (Miley Cyrus ft. Iyaz), it starts with an A both in the chorus and melody note, although it is really in G minor flat-7.