Does the spectral type of a star change as it ages? Since its temperature changes, I suppose the spectral type should also change, but I'm a bit confused about this. Also, is the spectral type entirely based on the star's spectrum? If yes, then why do we say O types are hotter and M types are cooler. I mean the statement is true but why not say that O types have very few lines and M types have a large number of lines? Or is it just a phrase stuck through history just like O type being early and M type being late stars (although this one is wrong)?
Yes, the spectral type changes with age. The spectral type is a function of temperature, gravity and chemical composition at the photosphere. All of these can change during a star's life.
A star spends most of its life on the main sequence and changes in temperature and gravity are relatively slow. But thereafter there are comparatively rapid changes. For example, the Sun will become a K-type giant when it is older.
Second question. Spectral type is based on the appearance of the spectrum. However it turns out that when you put the spectral types in the correct order, they are also a sequence of temperatures and gravity.
We say O stars are hotter than M stars because that is a physically meaningful thing to say. The appearance of the spectrum and categorisation is just taxonomy.