When you connect to a network, you generally have an IP address assigned automatically by the router. This process is necessary for devices to be able to communicate with devices on other networks. The key service that assigns IP addresses is called Dynamic Host Configuration Protocol or DHCP.
DHCP operates in a client-server model where a client broadcasts a request for an IP address to the local network and the DHCP server responds with by assigning an IP address. Typically, this happens as soon as the device is connected to a network, so it can start communicating as soon as possible. When IP addresses are issued, they are only leased for a certain amount of time, after which the DHCP server will reclaim the assigned IP address. To avoid this, hosts will start trying to renew their current IP address allocation after half of its leased time has passed. Generally, if a lease expires and a host is assigned a new IP address, it is assigned a random new one, rather than the same one as before.
On home and other small networks, the DHCP server is typically integrated into the router, as this keeps the number of devices down and reduces setup complexity. For big corporate networks, typically a single DHCP server will be used for a large number of networks, such as an entire building. In this case, DHCP relay agents are used on each interconnected network to forward DHCP traffic from hosts to the DHCP server, and to forward the response back to the original host.
Typically, a DHCP server offers connected hosts a random IP address from a configured IP address pool, however, it is possible to manually allocate an IP address to specific devices as well if needed. This is useful if there is a server of some sort on a network such as a NAS drive or local webserver. By manually assigning an IP address to these servers, they are always in the same place, and you don’t ever have to worry about their IP address changing and causing problems.