When do you gain life from lifelink when attacking?
You will gain the life at the same time that the damage is dealt. Lifelink simply changes what the effects of dealing are, to include gaining life. 

From the basic rule book:


  lifelink—If a creature with lifelink deals damage, its controller also gains that much life.


Note that some creatures, such as Horned Cheetah have a similar ability to Lifelink, but it works slightly different. As a triggered ability, Horned Cheetah's ability will go on the stack when damage is dealt, and ou will gain life after that ability resolves.

So with Lifelink... you are at 1 life, and your opponent is attacking you with 2 1/1 creatures. You can block one of them with a Vampire Nighthawk, but can’t block the other. When combat damage is dealt, you lose 1 life and gain 2 life simultaneously, so you do not lose the game.

If instead you had a Horned Cheetah to block with, then you would lose the game before you gain the life fro Horned Cheetah.