This is not exclusive to spectroscopy applied to astronomy but general.
Matter can interact with electromagnetic waves spanning a very wide range of frequency (energy). Also matter can emit electromagnetic radiation when in a kind of excited state.
Due to the internal mechanism of absorption/emission it happens that the spectral characteristics can be peculiar to the chemical nature and physical state of the matter under analysis.
This is basically what you are aware of.
Your concern likely arises by over-focusing on colour, that per se in not the most powerful identification tool, or even by thinking that the particular state of the matter does not count, or changes are neglected.
Let me take your example of leaves. The fact that they colour undergoes changes does actually means that their composition changes. This is already an information rather than a problem.
Imagine that observing a planet one detect, say, carbon monoxide, it does not mean that its atmosphere won't change. Eventually different observations would prompt a planetologist to ask himself why, perhaps concluding that a kind of volcanic activity or degassing is ongoing , just to say.
A planet looking green then brown on a regular pattern would certainly suggest the presence of plants or anyway photosynthesis based on chlorophyll, providing that the green spectrum closely resembles that one we know, and does not come from narrow spectral features that would otherwise indicate, for instance, the presence of chlorine.
In other words, probing a sample, in astronomy as well as on a lab bench, leads to information about that sample in that moment. This is quite philosophical rather than a problem in spectroscopy or spectroscopy applied to astronomy. Moreover spectroscopy doesn't come alone and certainly requires considering the scenario.
But at its core is the existence of peculiar features, they can be single lines or more or less complicated spectra, and those are linked to the composition of the sample, elemental or molecular depending on the frequency window used.
I am not sure if this answer your question, but again this passage
"this thing is absorbing this wavelength or colour, therefore it is this substance”
is both correct and wrong. It must be taken with a grain of salt, or things must be analysed in depth.
What is certainly true is that there are specific lines typical of elements, or other spectral features typical of molecules. Explaining why it is so is way more complicated (for me) to be done in few lines here. But the reason resides on the discrete electronic structure of matter, as well as molecular shapes combined with limitations on which transitions can take places. Combining the two leads to a high specificity.
Unfortunately not the best example for astronomy, but consider that a given compound virtually has its own IR vibrational spectrum, different from that of any other compound!
You might want to read about spectroscopy in general, and have a look at Wikipedia's Astronomical spectroscopy; Chemical properties