Bicycle derailleurs look like a complicated mechanism at first sight, but this is just a false impression. The basic principle behind them is rather simple and beyond the visual diferences they rely on the same mechanism, that of a mobile parallelogram.
The mechanics of a rear derailleur
Modern rear derailleurs are built as a parallelogram which has one end attached to the frame, while the other one has a mobile arm that guides and tensions the chain. The parallelogram is actioned thanks to a lever and a cable, which are in turn actioned by the shifters. Also, it is responsible for keeping the mobile arm parallel with the sprockets so that it aligns perfectly with them. The travel of the whole derailleur is limited by two adjustment screws.
The actual derailleur
The rear derailleur is actuated by the forces that the cable applies, bringing it in line with the sprockets, along with the chain. As the chain moves from a lower to a higher gear (or from a bigger to a smaller sprocket), the mobile arm withdraws, taking care of the extra chain length issue.
In the pictures below you can see the position of the derailleur when it is aligned on the biggest sprocket. The blue arrow indicates where the axle of the mobile arm stands, while the yellow lines reveal the levels of the smaller sprockets.
As you shift gears, the parallelogram moves in the direction shown by the green arrow and the mobile arm in the one pointed by the red arrow, thus tensioning the chain. When the derailleur is set on the highest gear, things should look like this.
One has to note that along the movement of the derailleur, the distance between the axle of the mobile arm and the teeth of the sprockets remains mostly constant. This happens because of a lateral tension in the chain that appears especially when shifting.
The derailleur lever (shifter)
When you shift gears, these are indexed according to the number of sprockets, so as to have a level that matches a certain gear. The actual mechanism is situated in the shifter, the lever that commands the derailleur what to do, and consists of some spring jacks (pawls) that allow only a certain length of cable to be pulled or released. Depending on the producer, every move of the shifter pulls or releases a specific length of the cable, this being the main reason of compatibility issues between different shifting systems. E.g., the SRAM shifters use the direct-pull technology (1:1 ratio), while the Shimano shifters have a 2:1 ratio, both having their own advantages, in the first case, the higher shifting speed, while in the latter, the increased confort and accuracy.
Another setback of the indexed shifters is the compatibility only with the sprocket cassettes with the according number of cogs. So, if you want more or less sprockets, you will have to buy a shifter designed for that specific cassette, or settle for lower functionality.