The origin of this phrase isn't clear and there are two possible ones. One is a figurative origin and the other is based off of a 15th century veal pie. But that origin has been labeled a 'nice story' and not very probable. You can read about the origin of the phrase here.
Using this phrase indicates that the person is very certain of an outcome and would be surprised if something happened differently and that surprise would cause them to do something so crazy like trying to eat a hat, which is impossible.