Shoot yourself in the foot is a phrase that comes from a time in the US when nearly everybody carried a side arm (gun). If you went to draw your gun from the holster, and you were not careful, you could accidently fire the gun too early and shoot yourself in the foot. If you did that, you had harmed yourself before your could use the gun to defend yourself and had been your own worst enemy.
This phrase has lived on and is now used anytime somebody causes themselves a problem.