How does Apple Music prevent you from keeping offline music after cancelling your plan?
They store the tracks in a different location than normal music and validate your subscription periodically when you play the files. 

I believe the check in are quite rare as the system only promopts to renew tokens if you don’t otherwise connect regularly. I’ve seen it maybe three times a year lately. Even being offline a month, the token isn’t expiring in my experience. 

This system seems designed to make it easy to re-up and give people latitude and runway in the mean time rather than to prevent any and all short term gaps. Clearly, if you’re online when you cancel, music would and should expire the music, but I’m not seeing that happen aggressively when you still pay for the service in practice or it lapses at the end of a subscription period.