Whenever I get to my final try, I exit out of the terminal and go back in to try again, but I am curious as to what happens if I choose the wrong password too many times and get locked out. Does it lock me out forever or is there a way to gain access in order to try and hack it again?
If you get locked out of any terminal while hacking, you should only get locked out for 24 hours. After this, it should be unlocked for hacking again. If there is a bed nearby, you can just sleep it off and come back to try again. Hacking terminals is quite easy in Fallout though, once you know how it all works, you should be able to unlock most of them without too much trouble. Here is a guide i wrote for hacking terminals this should give you all the info you need about hacking terminals in Fallout 4.
For the record, the mechanic has been unchanged since fallout 3, so the same rules apply to all 3 of the Fallout games that have been released so far.