The ester function formally results from the condensation of a carboxylic acid and an alcohol.
Esters are thus usually prepared by the reaction of carboxylic acids, or their derivatives, and alcohols.
Yet, there are more methods...
One of the simplest ways to get an ester is the boiling of a carboxylic acid dissolved in an alcohol with a catalytic amount of a mineral acid. Please, watch the whole procedure in the video.
The reaction proceeds by the typical addition-elimination mechanism, the C=O group being activated by its initial protonation by the mineral acid.
When the ester function is enclosed into a ring, its name changes to lactone.
Lactones are cyclic esters that are obtained by intramolecular reaction.
Hydroxyacids react intermolecularly at high concentrations leading to polyesters used as synthetic fibers in the textil industry.
Any ester can be turned into a different one by dissolving it into an alcohol in acidic medium. Here you are some examples:
Acyl Halides or Anhydrides and Hindered Alcohols
Acyl halides and anhydrides are so reactive that the acidic medium is not needed.
In the case of anhydrides, only one of the two C=O groups reacts.
Nucleophilic Substitution with Carboxylates
Carboxylic Acids and Diazomethane
Diazomethane, CH2N2, is the simplest diazocompound discovered by the German chemist Hans von Pechmann in 1894.
It is a yellow gas at room temperature when pure that nastily explodes.
It is therefore used in ethyl ether solution as a useful methylating agent at laboratory (not industrial!!!) scale.