Rome is 30km from the sea and so it needed a port.
The Romans developed a place on the coast where the River Tiber (which flows through Rome) met the sea. Most of the ships at that time were beached on the shore, so Ostia had to be on the coast.
(Much of the building we see in Ostia was done to the wishes of Hadrian, the emperor who also built Hadrian's Wall in Britain.)