This story begins more than 400 years ago with a technique called "hand in cap" which was a method for bartering items of unequal value.
As part of that transaction, an umpire would propose a sum of money that the person with the more valuable item should receive to make the deal fair. At that point, the two barterers would deposit money into a hat, reach into it, and then withdraw their hands either holding money or not to indicate whether they accepted the terms of the deal.
By the late 17th century, the word had morphed a bit, and the concept of making a lopsided contest more equitable was being used at the racetrack. Horses were handicapped given additional weight before the race.
From there, the word developed its more recent sense: "a disadvantage that makes achievement unusually difficult."