Alongside proximity, this is Gestalt psychology's other core grouping law. Proximity groups by distance; similarity groups by shared attributes — colour, shape, size — even across a scattered layout.
In UI, this shows up as always colouring destructive actions red, or using a consistent shape for tags of the same category. Consistently underlining and colouring links blue teaches similarity: "this whole set is clickable."
The shapes below keep their grid positions the whole time — only colour changes — yet same-colour ones start reading as a group.
When to use
When defining a colour or icon system, keep "same meaning, same colour/shape" consistent. Exceptions turn similarity into a false signal.