The main purpose of statistical mechanics is to give a microscopic derivation of macroscopic laws, including in particular the celebrated second law of thermodynamics. In recent years, there have been spectacular developments in this respect, including the integral and detailed work fluctuation theorems and the theory of stochastic thermodynamics. Here we give a brief introduction to these developments. In the first step, we derive the first and second law of thermodynamics for a Markovian stochastic process at the ensemble level, including two major advances: 1) the theory can be applied to small-scale systems including the effect of fluctuations, 2) the theory is not restricted to near-equilibrium dynamics. As an application, we evaluate the efficiency at maximum power of a two-state quantum dot. We also briefly discuss the connection to information-to-work conversion (Landauer principle) and the splitting of the second law into an adiabatic and non-adiabatic component. In a second step we formulate stochastic thermodynamics at the trajectory level, introducing stochastic trajectory-dependent quantities such as stochastic entropy, energy, heat, and work. Both the first and the second law can be formulated at this trajectory level. Concerning the second law, the crucial observation is that the stochastic entropy production can be written as the logarithm of the ratio of path probabilities. This in turn implies a detailed and integral work and fluctuation theorem, linking the probability to observe a given stochastic entropy production to that of observing minus this entropy change in a reverse experiment. The usual second law, stipulating the increase on average of the stochastic entropy production, follows as a subsidiary consequence.