Plaid Cymru has defeated Reform UK in a fiercely-fought Senedd by-election in Caerphilly, with defending party Labour trailing in third.