Your roof is designed to shed water in overlapping sheets, and then a chimney interrupts it: a masonry tower rising through a hole in that system. Flashing is how the hole gets sealed, and understanding its design explains exactly why it eventually fails.
The two-layer system
Proper chimney flashing is a pair of overlapping metal layers: step flashing, L-shaped pieces woven into the shingle courses, climbing the chimney’s sides like stairs, and counter-flashing, embedded into the masonry’s mortar joints and folded down over the step pieces. Water running down the brick hits counter-flashing, sheds onto step flashing, and rides the shingles away. When it’s right, it’s elegant. The uphill side should also have a cricket on wider chimneys, a small peaked diverter that keeps water from pooling against the back face.
The four ways it fails
Why it out-leaks every other component
The crown fails by cracking; masonry fails by soaking, slow modes. Flashing fails by opening a direct channel from roof surface to framing, so its leaks show up indoors faster and more dramatically: ceiling stains during active rain, drips in the attic, sign #6 on the urgency list. The diagnosis and fix are one visit; the differential is whether it happens before or after the drywall bill. Get it looked at.