I know that it means "run for your lives",
No, it means "run to a place of safety"
but why would running to the hills be a good idea to hide?
This is more a question about tactics than a language question.
Aren't the hills the easiest place for the enemy to spot the peasants/people who "run for the hills" or "run to the hills"?
Th earliest reference I can find is to a fox-hunt in England (RS Surtees "Ask Mamma" 1858) in which, of the fox, we have: “He’s for the hills!” exclaims Gameboy Green, still struggling on with a somewhat worse-for-wear looking steed."
And from Allen's Indian Mail and Register of Intelligence..., Volume 15 p314 --
May 19th 1857: After a sharp fire of about twenty minutes, the pirates began to jump overboard, and run for the hills, when the boats dashed in and boarded them.
In both cases, chasing something up hills is not easy. The thing/person being chased has the advantage of height. Also for humans, hills are more defensible.
Also, back then, the hilly areas were vast and sparsely populated and a good place to hide.
There is nothing to say that the hills did not have tree cover on them.
I think earlier references tend to be literal but the drama of the command was enough to take it into its more metaphorical meaning - although the two are often difficult to distinguish.