Welcome.
I had a quick look at the datasheet of the 7805 from ST (nb: there might be other manufacturers, I didn't checked), and there is no specification of what happens when turning on and off the input voltage. It is clearly not the intended usage of a voltage regulator (which expects the input voltage to be on (with sometimes changes in voltage), and the output current to sometimes change, and manages to keep the output voltage stable in those conditions).
What you are doing, is basically turning your device on and odd several hundreds/thousands times per second: it is not what the device is meant for. If you do it slowly enough, I expect halfway acceptable results (it's nothing other that turning on and off your device). How slow, I have no idea: 1 time every 10 seconds is very likely to be fine. One millions times per seconds is probably not fine. Where the limit is, I have no idea.
If it works for you, and it is a one off project without anything at stake, you can try it if you want, but no guarantee that it will continue working. Expect problems if you increase the PWM frequency. You might also run into trouble at very low duty cycle.
So now, what would be the typical approaches:
1) Voltage divider:
simulate this circuit – Schematic created using CircuitLab
This is the simplest solution, provided you need far less current on the 5 V PWM than what the 24 V PWM can provide.
The output voltage (i.e. 5 V PWM) will be the input voltage (24 V PWM) multiplied by R1/(R1+R2). With the resistors I chose: output_max=24 V * 10k/(10k+39k) = 4.9 V.
You can chose any other pair of resistors you want, as long as the ratio gives you the right output voltage, and the current I=24 V/(R1+R2) is bellow the maximum current your 24 V PWM can provide.
Note that the current I should be at least 10 times the current needed by the 5 V PWM (otherwise the voltage will be smaller than expected).
2) Zener diode + resistor.
simulate this circuit
We replace the lower resistor by a Zener diode (conducting with a fixed voltage, in this case 4.7 V, or blocked): the resistor limits the current (otherwise it's a short circuit).
This solution is more robust if your 24 V varies a lot, as the output voltage don't depends on the exact input voltage.
3) The power solutions:
If you expect to need to provide "significant" current to the 5 V PWM (more than the 2 solutions above, which are "signal only" solutions), then you will have to use transistors of one kind or another (or other integrated circuits). I will not expand on this option for now, as I don't think it's needed (and explanations are a bit more complex), but if you think you need >0.1 mA current, then just ask, and I will expand.
I would suggest you try solution 1 first (as you likely have already some resistors)