New Zealand failed to cope with Ali's reverse swing in a miserly bowled 17 overs of left-arm swing, and was all out for 262 late on the third day.
Left-arm spinner Zulfiqar Babar chipped in with 3-79 as New Zealand's top order found it tough in its first ever Test in the United Arab Emirates.
Pakistan, which declared at 566-3, didn't enforce the follow-on and stretched its lead to 319 by reaching 15 without loss at stumps.
Corey Anderson made a rapid 48 off 70 balls with eight fours, but he and Latham fell to Ali after putting on 83 runs for the fourth wicket, New Zealand's best partnership, during a brief aggressive batting spell in the second session.
Also Read
On his overnight five, Latham's hard push was dropped by Azhar Ali at forward short leg, and on 32, wicketkeeper Sarfraz Ahmed couldn't hold onto a thick edge off Mohammad Hafeez's offspin.
The left-hander profited from the two lapses, and showed lot of maturity in only his fifth Test since making his debut against India this year.