It is hard to give a general answer, a lot will be highly domain specific. It's also hard to answer as engineers don't work in a vacuum. Depending on wether one designs a product to be mass-produced or a one off industrial plant, safety relevant specifications may arrive at the engineers desk from the client, from an official ordinance, their professional conscience or even from their marketing department. The designing engineers may then use a few tools to arrive at a reasonably safe design, these include industry codes and norms, simulations, testing and risk assessments.
For a lot of machines and components, you have codes that specify how the device is to be constructed. For example pipe class ratings tell you what wall thicknesses and flange types to use for many temperature and pressure ranges, a comparativly simple example.
Certain safety critical behaviors can be tested, a classical example would be a crash test dummy in a vehicle. This is not always viable or economical, AFAIK testing is common for complex, mass produced goods like cars where the costs of the test are ameliorated over many produced units.
Tests can sometimes be replaced or aided by complex simulations, this is again common in car design.
Depending where you live, every machine sold will have a CE marking, which (among other things) means the manufacturers did a risk assessment. Industrial plants, too, need one to operate legally (in theory). This means someone or many someones made a long list of all liekly hazards like burning on open flames, on hot surfaces, electrocution, cutting on sharp edges, injuries due to fast moving parts etc. For each risk, they will evaluate how often it will ocur, how many persons will be in danger, how serious the danger is, and if - in sum - the risk is deemd unacceptable, the design goes back to the drawing board to install further safeguards or remove the risk some other way.
The difficulty in answering this uestion is that the answer depends highly on the field you work in. You can crash a car into a wall and examine the test dummy, you can't test a nuclear plant the same way (too heavy) and to do it with a sawing machine would be plain dumb. Every domain has it's own standards.
Is there perhaps a component of "anticipating to avoid risk based on earlier experience"?
Previous experience is (hopefully) baked into the codes and norms the engineers work with (often very implicitly so, it must be said).
Last not least, there's many overlapping concerns regarding safety. The area I know best, plant design, you have the safety of the individual machines, you need to consider hazardous incidents (release of dangerous substances) as well as workplace safety, explosion protection, fire protection. The last three usually have their own dedicated expert who, at several points, looks over the design and it's execution.