Why does the European Union give subsidies to farmers? According to neoclassical economics, subsidies are a solution to correct for the existence of externalities in consumption or production. 

What is the economic rationale/justification for such subsidies in the EU? Which are the externalities subsidies are correcting for? Or are subsidies justified by other motives, e.g. political?
Markets are extremely poor at pricing events that have very low probability and catastrophic impacts. In part because we're very bad at estimating low-probability events: by definition, they're rare. And in part because the catastrophic impacts fall on pretty much all the population, so any single purchase has very widely-spread potential negative externalities.

And there are strongly asymmetric risks here - if civilisation collapses, it can take centuries to rebuild it. If one country falls under the control of another, it becomes extremely expensive and messy to reverse this.

As such, societies decide - in order to avoid those catastrophic impacts - to pay a kind of insurance premium. A high risk of some market distortion is an acceptable premium to pay, in order to avoid catastrophic loss.

Food is probably the most obvious market where a dramatic loss of supply could lead to catastrophic failure. (the carbon market is another) By subsidising farmers, we ensure the continuity of the domestic supply of food: this helps ensure the availability of food locally, so the population gets fed; and reduces the strategic international risks that would arise from high imports of essential foods.