Labour came third in the Caerphilly by-election, its first parliamentary defeat in the seat for 100 years.