Due to the properties of metals called malleability and ductility, we are able to bend, compress, shape/draw them into pipes, wires etc.
Due to hammering, there is a high stress/pressure on the area that is hit by the hammer. The stress/force makes the molecules of the metal to rearrange among themselves. The stress makes the metals give way to the hammer and the molecules move sideways, so the metal gets flattened.
The metals are heated usually before hammering, because at higher temperatures, the resistance to shaping is lower. Malleability is higher. The molecules of metal are more mobile.