So I found a village near where I originally spawned and I expanded on it. I thought I could keep it safe by building a 3-4 block high wall around the village, unfortunately this didn't help (now all my villagers are dead!). I still find mobs inside the city walls and inside of the university I built. The university has a basement, and although I usually keep the door to it closed I'll still find the occasional spider, zombie, skeleton warrior or even creepers.
So my question is how do mobs spawn? Does the presence or absence of light make any difference? Are other factors involved?