A camera sensor uses millions of tiny pixels (each megapixel has one million pixels!) When the shutter opens, a photosite is uncovered, and a small cavity collects photons (light particles). Photosites gather different amount of photons according to the bit depth (255 are collected for an 8 bit image).
A cavity cannot distinguish how many photon of a particular colour have fallen in, so this would give rise to a grayscale (black and white) image. Therefore, a filter is placed over every cavity, so only a certain colour can pass through.
A bayer array consists of alternating row of red and green and a row of green and blue filters. Having twice as much green helps the human eye perceive the image as less noisey and with finer detail because the eye is more sensitive to green wavelengths.
There is a process known as Bayer demosaicing,which converts primary colours into the final, full colour image. It does this by treating a 2×2 array as one unit. A 2×2 unit is made up of two green, one red and one blue photosite.
More information can be extracted by overlapping each of the 2×2 arrays, therefore achieving higher resolution.
Other demosaicing algorithms are available, such as X3 technology, where red, green and blue photosites are embedded in silicon.
Some more advanced cameras use cyan, magenta, green and yellow to demosaic, instead of the more common green, red and blue array. Some cameras even go as far as having two sensor, though in general, consumer priced cameras use the Bayer array.
During demosaicing, artifacts can occur. These are unrealistic patterns caused by a faulty reading. One classic example is chromatic abberation, or , more commonly ‘purple fringing’. The most common artifact is a moiré (pronounced moray). This may appear as stumped, or repeating patterns.
The whole sensor is not covered in photosites, and they are generally not arranged perfectly. In fact, space is left on the sensor to accomodate other technology, such as ‘microlenses’ which act as channels, directing the light to the photosites, and enhancing their photon-gathering ability.
You can also view my article on Sensor size- And how it affects your photography