Flu is caused by a viral infection. The virus can be easily transmitted via the air, so if you are in a closed area with someone who is sick then your chances of getting infected are greater.
Other conditions can influence your body's resistance to infection. Conditions like cold, weariness etc can lower the body's resistance. Staying warm, staying healthy ( a balanced diet , vitamins, regular sport etc) can increase the body's resistance to infection.