there are a number of ways... the low tech way is to add a switch at the brake lever or caliper, or even on the cable.
A step up in technology is to use a pendulum to activate a switch, or for a few bucks more, add an accelerometer to measure the force of deceleration, and add a transistor to switch power to the light.
I think I've heard of a design that used GPS to measure speed, and then activated the brake light when speed decreased.
The urge to create a bike brake light seems to outnumber the urge to buy a bike brake light, though. Lots of designs have been marketed, but I can't think of the last time I've seen one used.
Still, a web search pulls up a variety of designs that might serve as inspiration...
https://newatlas.com/sigma-bicycle-brake-light/38828/
https://bikebrakelight.com/
Add a Brake Light Switch to a Bicycle Brake Lever: 6 Steps (with Pictures)
Brake Lights for Bikes: 6 Steps (with Pictures)
Check out this clever new bike brake light (video) - Cycling Weekly
Steve in Peoria