History of fences on Wikipedia talks about it dating back to at least feudal times when discussing property rights and boundaries. Obviously much farther back when discussing defensive use cases (Hadrian's Wall and The Great Wall being two massive examples I can think of quickly).