With Latifi being 5 laps down at the end of the 2020 Hungarian Grand Prix, I wonder... if a safety car were to be deployed, and at some point lapped cars were allowed to overtake, could Latifi have gotten all 5 laps back, or would he only have been allowed to unlap himself once, therefore staying 4 laps down?
TL;DR: the main purpose of that rule is getting the lapped cars out of the way of the leaders during the restart by sending them to the back of the pack, it's not a rule allowing them to unlap themselves several times.
No, Latifi (or any other car in his situation) could not benefit from that in the way you described.
As the FIA's sporting regulation explains in article 39.12:
If the clerk of the course considers it safe to do so, and the message "LAPPED CARS MAY NOW OVERTAKE" has been sent to all teams via the official messaging system, any cars that have been lapped by the leader will be required to pass the cars on the lead lap and the safety car. This will only apply to cars that were lapped at the time they crossed the Line at the end of the lap during which they crossed the first Safety Car line for the second time after the safety car was deployed.
The main reason for that article is to get the lapped cars out of the way of the leaders. For instance, imagine a potentially thrilling restart having Hamilton in first place, Bottas in second but, between them, a slow backmarker Williams. That would ruin any chance of Bottas challenging Hamilton during the restart. That's the only reasoning for that rule, which was re-implemented in 2012 (article 40.12 back then) after being dropped in 2009.
You could argue that the situation you described is not specifically and explicitly mentioned in the rule, but since the very objective of that article is clear to FIA and all drivers/teams this very specific detail is not exactly necessary. On top of that it's worth saying that if your scenario were possible the article should mention it, indicating that cars several laps down could overtake the leader several times. Finally, to make your scenario even more impossible, the rule clearly says Latifi can "pass the cars on the lead lap", so if there is another car 1 or more laps down in front of him Latifi could not overtake it (not even once).
Therefore, in that situation, the Williams would overtake and rejoin at the back of the field, thus not getting in the way of the leaders anymore (again, the whole objective of that article), but it would be able to unlap itself only once.
I'm sure there must be a precedent for this situation, but I haven't been able to find one yet. However, my expectation is that dly is right: cars that are multiple laps behind can only unlap themselves once.
Allowing lapped cars to overtake generally causes a safety car period to get extended for at least one more lap, while they wait for the lapped cars to catch up to the back of the pack (or at least, get sufficiently far down the track from the leaders). Waiting for a driver who's five laps down to unlap themselves five times would make the safety car period last far too long, and would cause the other drivers' tyres to lose too much temperature in the meantime, which would cause problems once they got going again.
The simple answer to this question is yes. A lapped car has to unlap itself completely - otherwise it would still be regarded as an lapped car. The reason why lapped cars have to unlap themselves when a safety car has been deployed is one of basic sporting fairness - a principle that underlies the rules and regulations of all sporting competitions. Put simply: once a safety car has been deployed the field bunches up behind it. Any time advantage a competitor has gained over a rival is negated. For example, the lead car may have had a 20 second advantage over the second car before the safety car was deployed which is then reduced to zero behind the safety car. If lapped cars were not allowed to unlap themselves behind the safety car, they would still be at a time disadvantage once the race restarted - i.e. the time it takes to make up the lap, or laps, they have lost. Such a scenario would mean that lapped cars were being unfairly discriminated against simply because they were further behind the leader than cars which had not been lapped. Since the deployment of a safety car only benefits cars that find themselves behind the leader - who is the only car at a net disadvantage since he can only lose (and not gain) time vis à vis other competitors - it would be unfair not to treat all such cars equally. Misunderstanding of this application of the rules stems from attempts to interpret the minutiae of the rules rather than trying to understand the reason why such rules were created in the first place. Where there is lack of clarity the "spirit of the law" is the guiding principle.
Addendum: it was pointed out in a previous post that it would take forever for a car that had been unlapped 5 times to unlap itself. Of course it would, but that in itself is not a reason for the rules not to be respected. After all, those 5 laps represent the time advantage over the lapped car gained by the leader. Allowing the lapped car to completely unlap itself is the only way of addressing the fundamental disadvantage caused to the leading car when a safety car is deployed, and is the reason why the rules stipulate that that the safety car is to be brought in at the end of the following lap once lapped cars have unlapped themselves. This aspect of the rules enables the lead car to make an informed decision as to its strategy options for the remainder of a race. Thus, in the event of a car having been lapped 5 times and a safety car being deployed towards the end of a race, the lead car can calculate the likelihood of the race finishing behind the safety car and base its strategy upon that calculation. Without the security of that knowledge the lead car is at a further disadvantage compared to the following cars which are able to make their strategic decisions based on the actions taken by the cars in front of them. In such a scenario the lead car would not only have lost any time advantage it had gained up to that point, but would also be driving blind from a strategic point of view.
To my knowledge there are no rules or regulations of ANY sport that are conceived to place ANY participant at a fundamental competitive disadvantage. This would contradict the very notion of what sport is. In most cases the rules and regulations of a given sport focus on how that sport is to be "played." And, of course, those regulations are founded on the social construct of what constitutes a fair competition.