The impression I always had of Berlin was that it somehow happened to be right in the middle of West and East Germany. And the wall that divided the two countries happened to run through the city.
I. E. Imagine the wall is the diameter of a circle (germany) and Berlin sat at the center of that circle. But looking at maps, I realise that Berlin was actually quite deep in East Germany.
How was it possible for a non-communist entity to exist so deep in East Germany?
Why did the east Germans simply not capture it?
Was the wall just in Berlin? Or did a wall separate the entirety of East and west Germany? If it didn't, wouldn't it be easy for people to easily escape to non-communist Europe?
After WWII, Germany was split into four zones of occupation, and Berlin similarly despite being captured by the Soviets. The western part of the city was turned over to the western Allies while the western Allies turned over certain areas they had captured to the Soviets. This division had been agreed upon beforehand at the Conference of Yalta.
The French, American and British zones eventually formed the Federal Republic of Germany while the Soviet zone of occupation formed the German Democratic Republic. However, all four occupying powers retained certain rights in their former occupation zones and in Germany as a whole. One of those rights for the western Allies was the right to move about unhindered to Berlin along a number of specially designated roads and railways and three air corridors. So supplies could reach Berlin - in theory. In 1948 the Soviets declared a blockade of Berlin, which led to the Berlin airlift.
Capturing the western sectors of Berlin would have meant war between the GDR and the western alliance, and by extension between the Soviet Union and NATO. The western Allies maintained a military garrison in Westberlin, although these were more symbolic than actually capable of repelling a major attack. If the Cold War had turned hot, west Berlin would have fallen in short order.
The wall you are probably thinking of was just in Berlin and ran around the entire outer borders of west Berlin. The inner German border did not have a wall as such (with a few exceptions near villages and such) but was still heavily fortified with barbed wire, with extensive minefields and booby traps and constant patrols by armed guards. Escape through it was possible but very dangerous. Official numbers have more than 400 deaths of people trying to flee, with inofficial numbers more than double that. Warships patrolled the Baltic sea border, although some resourceful people managed to flee that way anyway.
The safest way to leave East Germany was to file for an exit permit (Ausreiseantrag) and wait for a few years while it was processed.
Sadly, the only sources I can point to are in German. Namely Über die Ostsee in die Freiheit, which covers the often hair-raising exploits of people trying to escape via the Baltic Sea.
As /u/Eisenengel has already noted, it was a fluke of the agreements made by the Allied powers during the war. This fluke led to a number of conflicts between the USSR and the Western powers throughout the early period of the Cold War. Notably, the Berlin Blockade of 1948 that /u/Eisenengel mentioned, the building of the Berlin Wall, and the Berlin Crisis. Each of these confrontations revolved around tangible concerns of the East German government conflicting with the ideological positions of the West and sprinkled with a great deal of brinksmanship and posturing.
I address the conflict in Berlin in this post.