Gram staining is a differential staining technique used to distinguish between gram-positive and gram-negative bacteria based on their cell wall properties. The process involves primary staining with crystal violet, forming a complex with iodine, followed by decolorization with ethanol and counterstaining with methyl red or carbolfuchsin. The color retention indicates the type of bacteria: gram-positive bacteria retain the violet stain while gram-negative bacteria appear red or pink after decolorization.