There are possibly two things going on here, short glitches (ns to µs) and much longer supply dropouts (ms to s).
You always need a bypass capacitor accross power and ground of a microcontroller. This keeps the local supply steady despite quite large very short term variations in the current the microcontroller is drawing. These variations are too fast for the power supply to regulate away. Also, the traces back to the supply have enough impedance at the high frequencies of these fast current variations to cause local voltage fluctuations even if the main supply was totally steady.
The other problem of longer term power supply dropouts has to be handled with significant energy storage somewhere. After all, insufficient power is coming in for some period of time, and the local storage has to make up the difference temporarily. The best place to put this is before the regulator. Let's say your regulator requires 2 V headroom. That means it will continue to produce 5 V out as long as its input doesn't drop below 7 V. That's 5 V less than the nominal 12 V in. A large enough cap on the input can hold up the input voltage of the regulator for some time after the 12 V input suddenly goes away. Put a Schottky diode in series with the 12 V input, then the cap afterwards. That prevents the input going low from discharging the cap.
For example, let's say you put a 1 mF cap on the input of the regulator (in addition of course to the small high frequency caps required for basic regulator operation as specified in the datasheet). Since you didn't say what your current is, we'll arbitrarily pick 100 mA in this example. Let's also say the Schottky diode drops 500 mV at full current.
The cap is then charged to 11.5 V during normal operation, and can drop to 7 V before the 5 V supply starts to drop. (4.5 V)(1 mF)/(100 mA) = 45 ms, which is how long the cap can keep things running after the 12 V input suddenly goes away.