The general mechanism is shown below. The resonating arrow (1) shows a resonance contributor of the diazo compound with a lone pair of electrons on the carbon adjacent to the nitrogen. The diazo compound then does a nucleophilic attack on the carbonyl-containing compound (nucleophilic addition), producing a tetrahedral intermediate (2). This intermediate decomposes by the evolution of nitrogen gas forming the tertiary carbocation intermediate (3).
The reaction is then completed either by the reformation of the carbonyl through an 1,2-rearrangement or by the formation of the epoxide. There are two possible carbonyl products: one formed by migration of R (4) and the other by migration of R (5).  The relative yield of each possible carbonyl is determined by the migratory preferences of the R-groups.
The epoxide product is formed by an intramolecular addition reaction in which a lone pair from the oxygen attacks the carbocation (6).
This reaction is exothermic due to the stability of nitrogen gas and the carbonyl containing compounds.  This specific mechanism is supported by several observations.  First, kinetic studies of reactions between diazomethane and various ketones have shown that the overall reaction follows second order kinetics.   Additionally, the reactivity of two series of ketones are in the orders ClCCOCH > CHCOCH > CHCOCH and cyclohexanone > cyclopentanone > cycloheptanone > cyclooctanone.  These orders of reactivity are the same as those observed for reactions that are well established as proceeding through nucleophilic attack on a carbonyl group.