Engine braking

Engine braking occurs when the retarding forces within an engine are used to slow down a motor vehicle, as opposed to using additional external braking mechanisms such as friction brakes or magnetic brakes.
The term is often confused with several other types of braking, most notably compression-release braking or "jake braking" which uses a different mechanism. 
Traffic regulations in many countries require trucks to always drive with an engaged gear, which in turn provides a certain amount of engine braking (viscous losses to the engine oil and air pumped through the engine and friction losses to the cylinder walls and bearings) when no accelerator pedal is applied.
Type.
Gasoline engines.
The term "engine braking" refers to the braking effect that occurs in gasoline engines when the accelerator pedal is released. This causes fuel injection to cease and the throttle valve to close almost completely, greatly restricting forced airflow from, say e.g., a turbo. The latter of which causes a strong manifold vacuum for which the cylinders have to work against—sapping much of the potential energy out of the system over time, and producing the majority of the engine-braking effect. This vacuum manifold effect can often be amplified by a down-shift, which induces a faster spinning drivechain to engage with the engine. 
Engine braking is a viable method of controlling the speed at which a vehicle travels downhill. By shifting to lower gears in a manual transmission or selecting the "second/low gear" mode on an automatic transmission, engine braking reduces the need to repeatedly apply the foot brake, lowering the risk of the brakes overheating.
While some of the braking force is produced due to friction in the drive train, this is negligible compared to the effect from the manifold vacuum caused by the air-flow restriction. 
On an automatic transmission, engine braking often spontaneously increases the engine RPM, causing a sudden revving to occur even without applying the accelerator pedal.
Diesel engines.
Diesel engines in personal cars provide little engine braking as they are not equipped with a throttle body and thus cannot draw a vacuum in the intake manifold.
In heavy vehicles the engine is often made to provide extra braking power to take some strain off the vehicle's regular brake system and to help avoid overheating the brakes. In its simplest form this consists of a butterfly valve that stops the exhaust flow. This is referred to as an exhaust brake and mostly found on older trucks. It has a limited effect, and more advanced systems as described below are near universal on newer heavy vehicles.
Compression release brake.
A compression release brake (also known as a Jacobs brake or "jake brake"), is the type of brake most commonly confused with real engine braking; it is used mainly in large diesel trucks and works by opening the exhaust valves at the top of the compression stroke, so the large amount of energy stored in that compressed air is not returned to the crankshaft, but is released into the atmosphere.
Normally, during the compression stroke, energy is used as the upward-traveling piston compresses air in the cylinder; the compressed air then acts as a compressed spring and pushes the piston back down. However, with the jake brake in operation, the compressed air is suddenly released just before the piston begins its downward travel. (This sudden release of compressed air creates audible sound waves similar to the expanding gases escaping from the muzzle of a firearm.) Having lost the energy stored within the compressed air, there is no "spring back" so the engine must expend yet more energy pulling the piston back down again.
This type of brake is banned or restricted in many locations where people live because it creates a machine gun like sound loud enough to disturb the peace, including waking people at night. It is very effective, however, and creates immense amounts of braking force which significantly extends friction brake life – a diesel engine can produce up to of braking power at 2,100 RPM.
Exhaust brake.
An exhaust brake works by causing a restriction in the exhaust, much like the intake throttle causes in a gasoline engine. In simple terms, it works by increasing the back-pressure of the exhaust. Nearly all of these brakes are butterfly valves similar to a throttle valve, mounted downstream of the turbocharger if there is one.
Exhaust obstructions.
Modern diesels are subject to many strict controls on emissions and often have many obstructions in the exhaust, which cause them to feel like they have some engine braking like a gasoline engine. The main ones are:
Two-stroke engines.
Engine braking in a premix two-stroke engine can be extremely harmful to the engine, because cylinder and piston lubricant is delivered to each cylinder mixed with fuel. Consequently, during engine braking, the engine starves not only of fuel but also lubricant, causing accelerated wear. Many old two-stroke cars (Saab, Wartburg, etc.) had a freewheel device on the transmission to make engine braking optional. Most two-stroke motorcycle engines since the 1970s have had lubrication by an oil pump, independent of the throttle and fuel system, such as Suzuki's Posi-Force system.
Applications.
As soon as the accelerator is released enough to slow the engine, engine braking comes into effect as long as the wheels remain connected via the transmission to the engine. A slipping or disengaged clutch, or a torque converter, would disengage the wheels or absorb braking energy. The braking force varies depending on the engine, and the gear the transmission is in. The lower the gear, the higher the braking effect due to higher rpm and the torque transferred through the transmission (higher torque is delivered from the engine in lower gears).
Engine braking avoids wear on brakes, and can help a driver maintain control of the vehicle. Active use of engine braking by shifting into a lower gear can help control speed while driving down very steep and long slopes, saving the brakes from overheating or excessive wear. If it is applied before the brakes have been used, it can leave the brakes available to make emergency stops. The desired speed is maintained by using engine braking to counteract gravitational acceleration. Potential transmission wear caused by engine braking can be mitigated by certain techniques. Slipping the clutch to complete a downshift wears the clutch plate as it slows the vehicle, doing the job of the brake pads or shoes. A well-executed rev-match in the same setting minimizes stresses on transmission components, so engine braking does the work of slowing the vehicle.
Improper engine braking technique can cause the wheels to skid (also called shift-locking), especially on slippery surfaces, as a result of too much deceleration. As in a skid caused by overbraking, the vehicle will not regain traction until the wheels are allowed to turn more quickly. If the driver reduces engine braking by shifting back up, or disengaging the clutch on a manual transmission, traction can be regained.
In hybrid electric vehicles, like the Toyota Prius, engine braking is simulated by the computer software to match the feel of a traditional automatic transmission. For long downhill runs, the "B" mode acts like a lower gear, using the higher RPM of the internal combustion engine to waste energy, preventing the battery from being overcharged. Almost all electric and hybrid vehicles are able to convert kinetic motion into electricity, i.e. regenerative brakes, but since the internal combustion engine is not used to slow the vehicle when using regenerative braking, it is not the same as engine braking.
Limitations.
Engine braking is a generally accepted practice and can help save wear on friction brakes. It is even used in some motor sports to reduce the risk of the friction brakes overheating. Additionally, fuel injection engines generally do not use fuel while engine braking. This is known as deceleration fuel cut-off (DFCO).
Although no longer in production in most countries, there are still plenty of carbureted engines in service, with which engine braking is counter-productive to fuel economy due to the lack of a DFCO mechanism. The cost of wasted fuel can well outweigh the gain of reduced brake wear.
Compression-release ("Jake") braking, a form of engine braking used almost exclusively on diesel engines, produces extreme amounts of noise pollution if there is no muffler on the intake manifold of the engine. Anecdotally, it sounds similar to a jackhammer, however the loudness is between 10 and 20 times the sound pressure level of a jackhammer (10 to 13 dB greater). Numerous cities, municipalities, states, and provinces have banned the use of unmuffled compression brakes, which are typically only legal in roads away from populations. In Australia, traffic enforcement cameras are currently being tested that automatically photograph heavy vehicles that use compression braking.