In this paper, we present a Lagrangian formalism for nonequilibrium thermodynamics. This formalism is an extension of the Hamilton principle in classical mechanics that allows the inclusion of irreversible phenomena in both discrete and continuum systems (i.e., systems with finite and infinite degrees of freedom). The irreversibility is encoded into a nonlinear nonholonomic constraint given by the expression of entropy production associated to all the irreversible processes involved. Hence from a mathematical point of view, our variational formalism may be regarded as a generalization of the Lagrange-dAlembert principle used in nonholonomic mechanics. In order to formulate the nonholonomic constraint, we associate to each irreversible process a variable called the thermodynamic displacement. This allows the definition of a corresponding variational constraint. Our theory is illustrated with various examples of discrete systems such as mechanical systems with friction, matter transfer, electric circuits, chemical reactions, and diffusion across membranes. For the continuum case, the variational formalism is naturally extended to the setting of infinite dimensional nonholonomic Lagrangian systems and is expressed in material representation, while its spatial version is obtained via a nonholonomic Lagrangian reduction by symmetry. In the continuum case, our theory is systematically illustrated by the example of a multicomponent viscous heat conducting fluid with chemical reactions and mass transfer.