Was it ever actually a real key? What did it open?
Which do you mean, that associated with the freedom of the city, a kind of honour given to revered citizens, such as in London usually referred to as "freemen of the city"? Or do you mean when a city is surrendered and delivers the keys to the enemy?