I am planning on bying a used PS Vita and it originally came with some game codes. If I buy it I will factory-reset the device. If the previous owner has already used the codes can I still use them to get the games after resetting the device?
Game codes have to be redeemed over SEN (Sony Entertainment Networks) and are account-bound. If the previous owner redeemed those codes, then they'll be able to download those games even after they switch their PS Vita or memory card so long as they still remember their SEN account name and password.
Codes that have been redeemed once cannot be redeemed again. The only way to play those games would be if they are still present on the console as well as the account that redeemed them. Therefore, a system reset means you can't play those games. Unfortunately, unlike the PS3 and PS4, the PS Vita does not support multiple accounts: each memory card can only hold a single account, therefore sharing games that way is not possible.
The only way you can play those games would be if the owner forgot to format their memory card and forgot to remove it as well. In that case you'd have access to those games; you could even re-download them if necessary. However, I'd advise against using someone else's account, since you could theoretically buy games using the previous owner's credit card (if they registered one), which could be illegal depending on local laws; it's also not a very nice thing to do.