In my job, I use a lot of shaft encoders. There are 2 basic types of shaft encoders: incremental and absolute (there are also resolvers, which differ slightly in their implementation). For the sake of simplicity (and trying to stick to the point of the question), I'll use absolute encoders as my example. So from this point, on, when I mention an "encoder," I'll actually be referring to an "absolute encoder."
An encoder tells a computer the approximate position of it's input shaft. It does this via the use of a glass disc with several concentric bands of "blacked out" and "non-blacked out" areas. Each band has a photo diode pair (sender and receiver) placed so that the blacked out areas block the photo diode pair as they pass, thus producing 1's and 0's. Each band correlates to 1 bit of a binary number that the encoder produces; the more bands, the more bits, and therefore, more accuracy.
Imagine an encoder with only one band in which half of the band is blacked out. This would only be able to tell you if the shaft was in the 1st half of rotation, or the 2nd. An encoder with 2 bands would have one band like the one described above, and the other would have 4 sections: 2 blacked out sections opposite each other. If this second band was positioned so that one blacked out section and one clear section aligned with the single blacked out section of the first band, then the encoder would have the capability of producing the following 4 binary numbers: 00, 01, 10, and 11. This encoder would have twice the accuracy of the first encoder and could thus tell the computer it's position to the nearest 90 degrees (rather than 180 degrees, as before).
The problem with this is that the change from 01 to 10 requires both binary bits to change state at the same time. If the alignment of the photo diodes is not exact, then the possibility exists that the computer could misinterpret the position during this transition. For example, if the encoder is on it's way from 01 to 10, a misaligned photo diode pair may cause the computer to see one bit change before the other. This could cause the computer to "think" that the encoder is at either 11 or 00 (incorrectly) rather than at 10.
Gray code prevents this from happening by rearranging the binary code so that only one bit changes at a time when transitioning from one "position" to the next. In this example (with 2 bits), the blacked out areas could be placed so that the binary sequence instead becomes 00, 01, 11, 10. Now, slight misalignment doesn't affect the interpretation of the signal produced because now we only expect one bit to change at a time (we're never worried that another bit has to "catch up").
The more bands (and therefore more bits) that an encoder has, the more potential for this kind of problem exists, thus the need for a gray code.