Because Isaac Newton decided that there were seven colors in the spectrum. Newton, in addition to his work on gravity, the laws of motion, and calculus, studied optics.
Newton thought that the colors of light, when white light is split by a prism, were analogous to the notes of a musical scale., so he made sure that there were seven of them. He initially described red, yellow, green, blue, and violet, but added orange and indigo to make the analogy closer. There's no sharp distinction between blue and indigo. and honestly even orange can be hard to distinguish in a rainbow. Cyan (blue-green) would in my opinion have been a better choice to add. But it's somewhat arbitrary: humans can distinguish hundreds of colors, many of which appear in the rainbow spectrum. (Also, a spectrum in a lab and a rainbow caused by rain droplets are somewhat different; the distinctions in the latter as not as sharp.)
See https://www.the-scientist.com/foundations/newtons-color-theory-ca-1665-31931, the source of this image from Newton's works, where he has labelled the colors with a musical scale.