Bernard Foley scored a controversial late penalty to edge Australia past Scotland and progress into the semi-finals where they will meet Argentina.