In general, the preterite refers to completed actions, and the imperfect refers to interrupted or repeated actions. So if you want to say "I did my homework", you would use the preterite, but if you say "While I was doing my homework, there was a power cut", the first verb ("was doing") is in the imperfect (and the second ("there was") is in the preterite).There are times when either tense could technically be correct. If you want to say that you lived in Spain for 6 years, you could use the preterite to emphasise the overall period of time and the fact that those six years have come to an end, or you could use the imperfect, which you would use to talk about your experiences during those six years.There are also many words which can hint to which tense to use. Words such as "while" and "during" stress the duration and so are followed by the imperfect. Sequential phrases such as "and then" and "first...second..." are followed by the preterite because they indicate discrete, completed actions. The context and words around the verb in question should always give you enough clues to work out which tense you need!