In this order:
- Manual router ID configured under ospf process
- Highest IP address of a loopback in the up state in the respective routing table
- Highest IP address of an interface of an up state in the respective routing table
It’s best practice to hard-code the RID with a loopback IP Address that’s routable.
From Cisco (Talks about EIGRP but it’s also the same for OSPF):
EIGRP router ID: Each EIGRP system in the network should be configured with a static and networkwide unique router ID. As a best practice, using one of the local loopback interfaces for better reliability and stability in an EIGRP-enabled network is recommended.