I try to understand the Viterbi algorithm for solving hidden Markov models. There is a pseudo-code of it in Wikipedia:
In the row that marked in blue (starts with $T_2$) I don't understand: how does it choose a state in case where there is more than one state with the maximum likelihood? in this implementation it will pick the first state that has the maximum, but it makes no sense.

Why not? Ties are esentially irrelevant, in the sense that any election will find the same global maximum. Hence to pick the first one is as correct as picking any other.