When should I use the perfect tense and when should I use the imperfect tense?

The perfect tense (il passato prossimo) is used to describe an action or situation in the past with a beginning and an end. The construction would be avere/essere + past participle.examples with 'avere':Il gatto ha mangiato il cibo (the cat ate the food)Lei ha lasciato il libro (she left the book)and 'essere':Mi sono svegliato alle sette (I woke up at 7)I genitori sono partiti in vacanza (the parents went on holiday)
The imperfect tense (l'imperfetto) is used to describe an action or situation in the past which the person used to do, i.e. an action or situation in the past that was ongoing. For regular verbs the construction takes the infinitive stem, without the -re ending, and adds -vo, -vi, -va, -vamo, -vate, -vano.Ci incontravamo ogni martedì dopo la scuola (we used to meet every Monday after school)Da piccola lei aveva i capelli ricci (when she was younger she used to have curly hair)
It is also used to describe how a person was thinking or feeling.Eravamo felici (we were happy)Il ragazzo aveva sonno (the boy was sleepy)
Both the tenses are used when discussing two things that were happening at the same time, where there is an "interrupting" event that uses the perfect tense, and an ongoing event which uses the imperfect tense. These constructions usually use the adverb 'mentre'Mentre guardavo la tv, è suonata il telefono (while I was watching the tv, the phone rang)Mentre lei studiava, la sua madre è entrata nella stanza (while she was studying, her mum entered the room)