when the Hastati started to fall back, what stopped them from being massacred by pursuing enemies?
Fighting in ancient battlefield did not happen like Hollywood or Total War games represent it - two bodies of man clashing together and slugging it out until one side brakes.
First off there was intervals between maniples so second line units could go forward and replace the units on the first line. This is sometimes presented as debatable, but I don't really see how. If Romans actually fought in unbroken lines there really is little point of having second or third line.
Secondly the actual hand to hand combat happened in intervals. Opposing lines closed together, did some fighting and then in organized manner or just naturally due to exhaustion and strain disengaged. After a short rest they renewed their strength and courage and could go forward again. This makes a lot of sense because clearly actual fighting takes a lot of strength. Just imagine holding up around 10 kg in form of shield and fighting for your dear life with a heavyish piece of metal. Battles lasted for hours and it obviously is not possible to do that non stop for such a long time.
And thirdly if hastati are pulling back in organized manner as opposed to routing they will do so in a formation, therefore opposing side does not really have an option to wildly run forward in attempt to massacre the retreating Romans. Formation can't really charge like that and as long as hastati is keeping in formation any individual wannabe hastati killers running forward could be dealt with. Massacre you imagine could only happen if hastati abandoned formation and runed for their lives.
Sources are The Complete Roman Army by Goldsworthy and The Oxford Handbook of Warfare in the Classical World by Don't Remember His Name.