If a monk dies after been married to a woman, who inherits his legacy? If a monk, after he made his eternal vows, quits his monastery and marries a woman by law and his 'marriage' with the monastery is not annulled by the pope, to which is his legacy (capital) going when he dies? To the monastery or to his wife?

Of course this is be answered by the rules of the RCC.

Would the answer be the same for a pastor/priest who had received his ordination?
A monk is not "married" to his monastery in any sense at all, so there is nothing to be annulled. If a monk wishes to leave the monastery, he can ask for an "indult of exclaustration" (canon 686 section 1), which can be granted by either the bishop or the pope, depending on what kind of religious institute ("order") the monk is in.

Once the monk is granted this indult, he is " considered freed from the obligations which cannot be reconciled with the new condition of his or her life" (canon 687), including the perpetual vows of celibacy and of poverty (is that its one of the vows of the institute he was a member of). If the former monk marries at this point, his responsibility is solely to his wife and his family. There is no question of any part of his inheritance going to the monastery unless he specifically wills it.