It takes some practice.
We were taught that if you put your nose right in the center of the image, and then let your focus go, and pull back from the image, that at a certain distance from the image (as your focus is still at ∞) various structures of the image will begin to resolve. So contrasts, similarities, and shades will all resolve at different focal lengths from the image.
It was rare that any one person would be able to pick up immediately upon all the effects perceptible in an image. I was able to pick up on certain shades of the color green that are used in contrast to red, but it took me a long time to get the shading of black-white (as in this optical illusion - and it is but one of many).
When we were tested on this, we would not be told what was similar, or where optical tricks were used, and we would have to pick them out of an image (and this was long before the internet, so we couldn't just go online to do a search for optical illusions to find images to study that had their illusions spelled out for us). So, it is a skill that can be learned. For me, eventually I had to learn how to focus upon each square with a different eye, while squinting, and letting the focus go back and forth between my right and left eye. eventually, I get the images resolved as a single shade as I go back and forth between my eyes.
Today I looked at the above illusion and thought, "Why do I keep thinking A and B are different colors? Obviously, something is wrong with how I am thinking about colors." I am being stupid when my I look at this illusion and I interpret the data in such a way to determine distinct colors. My expectations of reality and the information being transmitted and received are not lining up. If they were, the illusion wouldn't be an illusion.
The number 2 is prime; the number 6 is not. What about the number 1? Prime is defined as a natural number with exactly two divisors. 1 is an illusionary prime if you use a poor definition such as, "Prime is a number that is only divisible by itself and 1." Building on these bad assumptions could result in all sorts of weird results much like dividing by 0 can make it look like 2 = 1. What a tricky illusion!
An optical illusion is only bizarre if you are making a bad assumption about how your visual system is supposed to be working. It is a flaw in the Map, not the Territory. I should stop thinking that the visual system is reporting RGB style colors. It isn't. And, now that I know this, I am suddenly curious about what it is reporting. I have dropped a bad belief and am looking for a replacement. In this case, my visual system is distinguishing between something else entirely. Now that I have the right answer, this optical illusion should become as uninteresting as questioning whether 1 is prime. It should stop being weird, bizarre, and incredible. It merely highlights an obvious reality.
Addendum: This post was edited to fix a few problems and errors. If you are at all interested in more details behind the illusion presented here, there are a handful of excellent comments below.