Perhaps unintended consequences.
This term can refer to positive, neutral or negative outcomes from a purposeful act. Wikipedia describes the last type
A backfire or perverse effect contrary to what was originally intended (when an intended solution makes a problem worse). This has been dubbed the 'cobra effect', after an anecdote about how a bounty for killing cobras in British India created a perverse incentive for people to breed cobras.
As noted, cobra effect might cover it, but that seems a bit obscure (I had never heard of it before).
As also noted, the term backfire is sometimes used
(Of a plan or action) rebound adversely on the originator; have the opposite effect to what was intended:
overzealous publicity backfired on her
Oxford Dictionaries Online
Finally, the phrase, the best laid plans ... is sometimes offered as an observance of the situation you describe. It is a slightly modified shortened reference to the Robert Burns poem, To a Mouse
The best laid schemes o' Mice an' Men,
Gang aft agley,
An' lea'e us nought but grief an' pain,
For promis'd joy!
(The best laid schemes of Mice and Men
oft go awry,
And leave us nothing but grief and pain,
For promised joy!)