Everything, all solid objects anyway including ships and rocking chairs, have what is called a center of gravity. That is the point where gravity seems to exert its influence on the object. To locate the center of gravity of a rocking chair, get down on the floor, sitting or on hands and knees will do, and face the side of the unoccupied, unrocking chair. You will notice that the rockers each contact the floor at a single point. The center of gravity of the chair must lie directly above that point. If it were off one way or the other, gravity pulling the chair down and the floor pushing the chair up would tip the chair forward or back.
Now reach out and rock the chair slightly. Notice what happens to the point of contact between the rockers and the floor. It moves back when the chair rocks back and forward when the chair rocks forward. The rockers are shaped so that when the chair tips back, moving its center of gravity backwards, the point of support, where the rocker touches the floor, moves back even farther. Because the center of support always stays outside the center of gravity, the down pull of gravity and the up lift of the floor combine to tend to straighten the chair up whenever it is disturbed from its resting state.
Like a rocking chair, a ship has a point of support called its center of bouyancy. When you place a steel ship in the water it will sink. Sink that is until the weight of the water displaced by the ship equals the weight of the ship and everything in it. The geometric center of that displaced volume is the ship's center of bouyancy. That is the point where the bouyant force of the water seems to act on the ship. It is similar to the point where the rocker rests on the floor in the rocking chair example.
Now if you, or some higher power, reach out and rock the ship as we did the rocking chair, the shape of the displaced volume will change to conform to the shape of the ship's now tipped hull. The hull is designed so that the center of bouyancy moves father than the center of gravity when tipping takes place. The combination of the upward force of bouyancy at the center of bouyancy and the downward force of gravity at the center of gravity tends to tip the ship back upright again.