The trick here is to look at squares A and B. B obviously appears to be a much lighter shade of gray than A. However, if we isolate the two squares from the larger context, we can actually see that they are the same shade of gray. This gif below the jump illustrates that point:
Now one might ask why our visual system has this defect, but as the discoverer of this illusion notes, this illusion actually demonstrates how powerful our visual system is,
As with many so-called illusions, this effect really demonstrates the success rather than the failure of the visual system. The visual system is not very good at being a physical light meter, but that is not its purpose. The important task is to break the image information down into meaningful components, and thereby perceive the nature of the objects in view.So, the reason the illusion fools us is not because our visual system is defective, but because our visual system has not evolved to be a photometer, but has instead evolved to give us the information we need to navigate our environment. This again demonstrates that our perception of the world is not a direct copy or image of reality, but is instead constructed by our brains to serve specific purposes for whihc the brain and those perceptual systems have evolved. This is why we always need to be suspicious of eye-witness reports and our own direct perception. Even when our visual system is functioning perfectly, it can still systematically succumb to these perceptual illusion.
For more interesting illusions and explanations of them, visit this link.
The image and the .gif comes from Wikipedia.
Update: And here is a really cool video demonstrating this illusion: