At first, most bacteria cells will take up the crystal violet. The lugol solution stabilizes the previous stain with the peptidoglycan wall in bacteria containing one cellular membrane and a thick wall of peptodiglycan (called monoderms or Gram-positives). When the solvent (ethanol) is added, the diderm organisms (or bacteria containing two cytoplasmic membranes, or gram-negative) lose their outer lipid layer and alongside most of the stain, as they contain a very small peptidoglycan layer unable to capture the crystal violet - iodide complexes. To be able to see them under microscope, a counter-stain is added making diderm organisms visible again, with the pink color of the safranin reagent (see Gram Staining protocol, 1981).
There are bacteria classified as "acid-fast" which are not stained with this protocol. If you need more information about them, you can read the paper from Bayot, Mirza and Sharma (2023).