When do we use IATA codes and when do we use ICAO codes?
It helps to know the objective of both ICAO and IATA to understand when which code is used. 

ICAO (International Civil Aviation Organization) is a UN-body which focusses on international harmonization of civil aviation regulations. 

ICAO codes are used for "official" purposes such as Air Traffic Control; E.g. flight plans use ICAO codes for airports and airline flight identification.

IATA (International Air Transport Association) is a trade association that focusses on making air traffic businesses safe, secure, reliable and efficient. 

IATA codes are mainly used for ticketing. E.g. travel itineraries use IATA codes for airports and IATA flight numbers.