When armies stay motionless on the world map for a certain amount of time (one turn for Napoleon: Total War and two for Empire: Total War) and is attacked, certain units gain the ability to place useful objects on the field before a battle commences. Earthworks are one such option.
Though described as trenches, earthworks are depicted in the games as a downward sloping mound behind which infantry can shoot--essentially, a temporary wall. Earthworks greatly decrease the amount of damage incoming artillery, musket and rifle fire deal. A regiment using earthworks to their advantage can hold off superior forces for quite some time in a musket duel. However, earthworks do not protect from melee attacks or weapons with an arced trajectory, such as grenades or howitzer fire.
Generally, units that can deploy earthworks are militia and line infantry. Most units have the option between choosing Chevaux de Frise and earthworks. Like chevaux de frise, earthworks can be stretched out by stretching out the regiment deploying them. There is no disadvantage to doing this, and indeed it can be beneficial to do so to increase the defenses of an area. Compared to chevaux de frise, earthworks offer minimal, if any resistance to cavalry, but are far superior as defense against enemy fire. Certain units, particularly light infantry and skirmishers, have also have the option of deploying Fougasse, a primitive land mine that is far more offensive in nature. The lives saved by earthworks over time, though, may prove more valuable than the casualties inflicted by Fougasse depending on the circumstances.
Most light infantry and skirmishers do not have access to earthworks. No cavalry has access to earthworks (or any other pre-deployment options). Artillery do not have earthworks, but can deploy Gabionades, which provide similar benefits.