Peter Sagan produced a brilliant burst to win an enthralling sprint finish on Stage 5 of the Tour de France.