When do you gain the life from lifelink when attacking? Is it right when the attacks happen, or is it at the end of your attack phase?
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.
The other answer is good, I just like to reference the comprehensive rulebook. Rule 119.3 details the effects of damage being dealt. For instance, 119.3a says
Damage dealt to a player by a source without infect causes that player to lose that much life.
And 119.3f says
Damage dealt by a source with lifelink causes that source’s controller to gain that much life, in addition to the damage’s other results.
So a player losing life from being dealt damage, and a player gaining life from dealing lifelink-damage, are both treated equally by the rules, and happens simultaneously as an integral part of what "deal damage" means.