The mechanism of Thioflavin S staining involves the dye binding to the beta-sheet structure of amyloid fibrils. This binding causes a shift in the fluorescence emission spectrum, allowing the amyloid deposits to be visualized as bright green or yellow under UV light. The specificity of Thioflavin S for amyloid proteins makes it an effective marker for identifying these structures in various tissues.