It is a conjuction that means during the time that or at the same time that:
She was watching television while we were reading the magazine
I didn´t notice it while she was there
They danced while the music was playing
*While is often followed by verbs in the past continuous tense
It is a preposition that means throughout the duration of:
Wild boar usually travel long distances during the night
She lives in Spain during the winter
My purse was stolen during the concert
I head strange noises during the morning
It is a preposition that means during the continuance of. It is followed by a period of time.
I have lived in Madrid for three years
They haven´t seen their families for a long time
The play lasted for over three hours
*For is often used with present perfect, although it is true that it can be used with past simple when talking about a completed action in the past.