The core idea regarding the accelerometer is the following:
if you take any flight of an object and calculate the average experienced acceleration over the whole flight (ignoring gravity as a factor for a second), that average should add up to zero. Here I'm assuming that the velocity at the beginning and end of the flight is the same, which is usually the case: the drone powers on while still and powers down also while still. If you add acceleration due to gravity back in, then that will be the average: g + 0 = g.
Additionally, most utilitarian/autonomous drones spend most of their time hovering or moving in a straight line at a constant velocity, during which there is also almost zero measured acceleration besides the gravitational component.
Of course, while the aircraft is actively maneuvering, the acceleration readings will be way off and can't be trusted for getting an accurate attitude(=orientation) estimate.
However, the IMU has two different kinds of sensors; in addition to the accelerometers it also has three solid-state gyroscopes built in, which measure angular velocity (rotation rates) directly. Those rates can be integrated to get the current orientation very accurately in the short term. In fact, gyroscope-based attitude readings are actually the main source of attitude information for any aircraft, and accelerometer-based readings are secondary.
The biggest problem with gyroscopes is error accumulation: since their readings are being integrated (i.e. added together) and every sensor has a small persistent error, the errors add up over time and and in the long run the accumulated error can become quite large.
To get the best of both worlds, you could, through a process called sensor fusion, combine data from both of those systems into one "fused" measurement that is accurate both in the short and long term.
There are multiple different sensor fusion algorithms in use; Arguably the most popular ones are the Kalman filter and the Madgwick filter. Both involve high-level math, but the basic idea could be approximated this way: at each step, you take the current gyro-based measurement and correct it a tiny bit towards the current accelerometer-based measurement. This way the gyro integration error is being constantly compensated and doesn't grow, and the accelerometer-based error can't throw the measurement off too much at any given moment since the corrections based on it are small.
All that said, if you do maneuver a lot, the algorithm can be fooled. If you turn on the respective feature on a betaflight quad, fly several minutes of hard freestyle and then turn on angle mode, the quad is likely to tilt hard to one side and think that's the upright orientation. So that (in addition to energy conservation and simplicity) is another reason why autonomous drones tend to fly in a straight line most of the time, and also why some acro pilots turn off the accelerometer entirely on their drones.
Other sensor fusion algorithms can be used for different kinds of readings: for instance one could fuse GPS readings with accelerometer readings and get a position estimate that is more accurate than GPS (because the IMU has better resolution) but doesn't drift (because GPS readings compensate for it) and can continue to function if the GPS fails in flight for some reason.