While moving, Android calculates the orientation of phone using the GPS data, compass, and data provided by the gyroscope. That's why the orientation is usually correct while your are moving. Once your are standing, the gyroscope as well as the GPS data don't change anymore, so your phone has to rely on its compass.
First of all, not every Android phone has a compass, so if you perceive orientation issues while standing, this might be due to a missing compass. If you are not sure if you have compass, you can find out by using the CPU-Z app. Good news is, that calibrating your compass is very easy.
Compass issues with smartphones are a well known issue and can appear on Android as well as iOS devices. The issue seems to be generated by electric fields of the smartphone or your surrounding. But calibrating your phone is very easy. There are two methods, either turning your phone along every axe, or performing a figure 8 like motion with your smartphone in your hand. You might have to repeat the process after some time once your compass starts to behave weird again.