Ear wax is not really wax at all - its name comes from its waxy texture and it is only produced in the outer third of the ear canal. This cartilaginous portion of the canal has a thin skin lining that is always renewing itself. As the dead skin cells drop off they combine with viscous secretions from the ceruminous (sweat) and sebaceous (oil) glands. So, ear wax (cerumen) consists of shed skin, keratin, fatty acids, alcohols, cholesterol and squalene.
Ear wax protects one of your body's most useful senses in a number of ways. It acts as a natural barrier to foreign bodies, such as dirt, bugs, bacteria (and other more obscure objects!), from reaching our ear drum. Being a natural lubricator it protects the skin in the ear canals from drying and cracking which would leave them prone to infection. Its antimicrobial properties means that it also packs a punch when it comes to fighting bacterial infections.
You may be surprised to find that the ear is in fact self cleansing! A process called epithelial migration moves skin cells along your ear canal, gathering up debris, all the way to the outer ear where it will eventually just simply fall out as a ball of wax. Some people produce more wax than the migration process can handle and/or routinely push ear wax back into the canal (stop using cotton buds!) which leads to a build up of excessive wax.