Kedoshim Tihyuh- Holy You Shall be
Rashi explains that this means having borders or 'fences' to distance ourselves from Arayos (Forbidden relationships etc.)
The Ramban takes a different approach but along the same lines and explains that this is referring to being separated and distanced from permitted worldy pleasure in general, since they can lead to stumbling into sin, and even if not, they still have the potential to leading to a life which is totally empty of spiritual content.
Both Rashi and the Ramban take the position that the Torah is coming to tell us to set-up separations- a “fence”.
The classic approach to understanding a “Geder” fence, is that it comes to forbid something- something that would have been permitted if not for the gate and is now forbidden. Obviously this is for our benefit so that we don't fall and stumble on something that can cause us damage whether spiritually or physically.
There is another way to see it. A fence can be seen as something that comes to bring closer and permit that which would have otherwise been forbidden and distanced.
Unfortunately there are millions of kilometers around the world that are totally off limits because of the fear of landmines. Since the exact location of land mines are not always known and people recognize the great danger they pose, large areas of land have been cordoned off and deemed inaccessible including areas that may not contain landmines. This causes a tremendous economic and social loss and makes life very difficult for these people. Many of these locations DO NOT have have gates but are still avoided because of the great danger they pose.
If the exact location of the land mines would be known then a simple fence just five away (to avoid accidents) would be enough and most of the unaccusable land would become “permitted” for use and accessible once again.
Another example of how the “fence” used correctly actually brings closer- is a zoo. No matter how large a city is, animals cannot be allowed to roam freely- humans and animals cannot co-exist so close. However if we introduce proper fencing (a zoo) then humans can enjoy the pleasure of visiting a zoo.
One more example is the “Mechitzah” at first glance is seems like it’s there to separate and distance but in reality it’s there to bring closer and permit. Men and women cannot daven together intermingled, so instead of making two different shuls one for men and one for women we put the “Mechitzah” which allows them to Daven in the same room and take part in the same Minyan
To summarize: When we recognize danger we will instinctively avoid it and usually marginalize out a much greater distance and area that does not need to (or should not) be avoided. If we introduce a proper “fence” then the area of concern is clearly blocked off which allows us more freedom near and around what should be avoided, without risking stumbling on it.
(Based on R' Shimshon Pincus Z"L)