World leaders have agreed a timetable to pull the majority of foreign troops out of Afghanistan by the end of 2014.