On a commercial airliner, what is the purpose of the flaps? I understand that depending on which direction they move in, they can be used to decrease airspeed, but I don't know when flaps are used. Are flaps used before takeoff, after takeoff and on approach?
Flaps change the camber of the wing's airfoil. This in turn changes the zero-lift angle of attack, increases the maximum lift potential and, in case of fowler-type flaps, the wing's area.
Airliners use flaps on both take-off and landing, and most of the time they are only partly deflected. Full deflection is only set on final approach to reduce the landing speed as much as possible.
When simple camber flaps are used, the drag increase is negligible for small deflections (±10° for a 20% flap), and they can be used for both take-off and landing. Other types of flaps are designed to increase drag (split flap, Zap flap) and should be used only for landing. If flight speed stays constant, a flap deflection will only change induced drag if it redistributes lift over the wingspan. Modern gliders use camber flaps inboard and flaperons (a combination of aileron and flap) outboard, and changing flap settings will not affect induced drag.
Generally, the purpose of flaps is to shift the minimum airfoil drag to the desired lift coefficient $c_L$. Especially laminar airfoils have a pronounced range of lift coefficients with low drag. The pilot should attempt to stay within this region of minimum drag, and flaps give him/her the means to achieve this. See below for an example of an airfoil at a Reynolds number of 1 Mio.
Flaps for lift increase work the same way, but change camber much more. Normally, they need to be combined with leading edge devices (slats) which change camber at the airfoil's nose to unfold their full potential. Below are some examples for flaps, in combination with proper leading edge devices. Note that the solid outline of the fowler flap is the position for landing, while the dashed outline is the position for take-off. $\delta$ is the relative chord of the flap.
Junkers flaps were the best way to control big aircraft in the days before more elaborate force compensation mechanisms were developed, and split- and Zap flaps were very popular in the 1930s. They are still used on smaller aircraft today. The simple Fowler flap is used on the C-130, and airliners use slotted flaps like the ones in the lowest two rows of the table above.
Besides increasing drag, powerful flaps change the pitching moment of the wing by shifting the center of lift backwards, which requires sufficient tail size and control power. On airliners, the incidence of the full horizontal tail can be adjusted in order to create enough downforce to counteract the pitching moment. Note on the picture of the An-70 below that the stabilizer has a slat which operates in the opposite direction to that of the wing.
Flaps increase lift and drag by changing the camber of the airfoil, and allow lower airspeeds. There is also an effect of shifting the center of pressure (C of P) aft. There may be an immediate decrease in airspeed due to the increase in drag (see the Wikipedia article linked below) until the aircraft regains stability by nosing down. There are some incorrect discussions online, such as one answer on this page.
Flaps are frequently used when landing because they lower the stall speed and increase the angle of descent. The pilot will also have better visibility due to the lower nose position caused by the aft C of P.
Flaps increase both lift and induced drag, but a small amount (e.g. 10 degrees) of take-off flaps may provide enough extra lift to allow the plane to become airborne sooner, without a large increase in drag, allowing for a shorter takeoff roll. Whether takeoff flaps can be used is declared in each aircraft's standard operating procedures.
I found these resources that I like:
The purpose of trailing edge flaps and leading edge slats is to alter the wing profile in such a way that it generates more lift at low airspeeds, while lowering the stalling speed. This makes it possible for the aircraft to take off and land at lower speeds than would otherwise be necessary. From the design perspective the inclusion of flaps makes it possible to optimise the basic wing section for the best performance in cruising flight.