Even though photons have no mass, they are still affected by gravity. That's how we can see black holes - by the way they distort the light going near them. The reason nothing can escape a black hole is that, within the event horizon, space is curved to the point where all directions are actually pointing inside. The event horizon of a black hole is where gravity is such that not even light can escape.