NHS collapse was only “narrowly avoided” due to efforts of staff who put themselves at exceptional risk, the inquiry says.