The direction is calculated along the shortest path on the globe (the great circle) from your place to the Kaaba in Makkah (21.4225° N, 39.8262° E). The calculation is exact; the limit is your phone's compass. Phone compasses can be off by several degrees near metal, cars, speakers or electric wiring. Move away from them and wave the phone in a figure-of-eight to calibrate it.
Twice a year, on about 28 May and 16 July, the sun is directly above the Kaaba at Dhuhr time in Makkah (around 2:18 pm Pakistan time on 28 May and 2:27 pm on 16 July). At that moment, the shadow of a straight stick points directly away from the Qibla — a simple way to check your mosque's direction.
For a new mosque, ask a qualified person to confirm the direction on site.