If you had set up an Account Recovery Contact ( Set up an account recovery contact - Apple Support ) you can also get a code that way.
In a situation where a non-functioning device with cellular service was the only trusted number, transfer the cellular service to another device so you can receive a verification code on the other device instead.
If you can’t sign in, get access a trusted device, reset your password, or receive verification codes, you can request account recovery to regain access to your account. This article explains how to recover your Apple ID after a waiting period - How to use account recovery when you can’t reset your Apple Account password - Apple Support One tip to using account recovery it to look for options in small blue letters beneath the normal log-in items if you cannot complete the requested information. The post by shoeluvr13 at this link provides additional detail about the account recovery process: iCloud account recovery with new phone nu… - Apple Community
You should consider making sure you have multiple trusted devices and telephone numbers (ask a friend or a relative).
You may also wish to read the articles 'Two-factor authentication for Apple ID' ( Two-factor authentication for Apple Account - Apple Support ) and 'Get a verification code and sign in with two-factor authentication' ( Get a verification code and sign in with two-factor authentication - Apple Support ). Various sections contain information that is useful in understanding about the trusted devices (an iPhone, iPad, or Mac that Apple knows is yours) system, and how to get a verification code on a trusted device or at a telephone number.