Halteres help balancing and improve flight. They help flies to perform their fast aerobatics. In addition to providing rapid feedback to the muscles steering the wings, they also help stabilise the head during flight.
Every vibrating object tends to maintain its plane of vibration, a result of the Coriolis effect. If the body of the insect changes direction in flight or rotates about its axis, the vibrating halteres exert a force on the body. The insect detects this force with sensory organs at the base of the halteres.
The two halteres vibrate in different planes, each forming an angle of about 45 degrees with the long axis of the insect. This increases the information got from the halteres.
In Diptera, the formation of the haltere during metamorphosis is dependent on the homeotic gene Ultrabithorax (Ubx). If this gene is experimentally deactivated, the haltere will develop into a fully developed wing. This is an excellent illustration of an important mechanism of evolutionary development; a simple homeotic gene change can result in a radically different phenotype.
The Strepsiptera, small insects parasitic on bees, wasps and cockroaches, also have halteres. We know the evolution was independent because in the Strepsiptera the forewings evolved into halteres while, in the Diptera, the hindwings changed into halteres. This is a classic case of convergent evolution.
Grimaldi and Engel say halteres have evolved at least seven times in insects, and "in most cases it is known to be associated with improved manoeuvering in flight".