In California, its legal if you have the right type of headphones, per
"(d) A person wearing personal hearing protectors in the form of earplugs or molds that are specifically designed to attenuate injurious noise levels. The plugs or molds shall be designed in a manner so as to not inhibit the wearer's ability to hear a siren or horn from an emergency vehicle or a horn from another motor vehicle. "
I recall a time when riding with earplugs was not legal, but my hearing is more important than a hypothetical ticket, so I wore them anyway. I'm not even sure how you'd get caught - the last time I received a performance award from the CHP, I didn't even remove my helmet.