Increasing ADC resolution by supersampling on a successive approximation ADC
Atmel has very clear application note about increasing ADC resolution by oversampling with sources in C.

Description in PDF is here, sources are on Atmel website.