Let's imagine that you're doing a hamstring stretch (as that's the one that usually elicits the most noise in class!).
As you extend your knee, you start to feel that familiar painful feeling in the back of your leg. This is your nerves sounding an alarm to warn you that they're not certain that this level of extension is safe for you.
As you hold the stretch, you notice that the sensation eases off. This is when I always say that your nerves have got bored and wandered off.
But it's more likely that as they realise that nothing bad is happening, they stop sending the pain message.
You're then able to extend the stretch a little more. the sensation comes back; the nerves realise that nothing bad is happening; they switch off.
This means that if you stretch regularly, your nervous system learns that this level of extension is safe and over time, you are able to extend further.
Are the muscles actually getting longer during the stretch? Yes, some studies using ultrasound indicate that the basic units of the muscle (sarcomeres) do actually lengthen and become more pliable for an hour or so after stretching.