The purpose of flux is to facilitate the soldering process. The main obstacles to a successful solder joint is an impurity at the site of the joint consisting of dirt, oils or oxidation. The impurities can be removed by mechanical cleaning or by chemical means, but the elevated temperatures required to melt the filler metal (the solder) encourages the work piece (and the solder) to re-oxidize. This effect is accelerated as the soldering temperatures increase and can completely prevent the solder from joining to the work piece. Some fluxes go beyond the simple prevention of oxidation and also provide some form of chemical cleaning (corrosion).
For many years, the most common type of flux used in electronics (soft soldering) was rosin-based, using the rosin from selected pine trees. It was ideal in that it was non-corrosive and non-conductive at normal temperatures but became mildly reactive (corrosive) at the elevated soldering temperatures.
Fluxes also act as a wetting agent in the soldering process, reducing the surface tension of the molten solder and causing it to flow and wet the workpieces more easily.