I am thinking of buying a small gift, like coffee, for a teaching assistant, who helped me solve problems in a particular subject, even outside of work time, to thank for the effort.
Is this appropriate or even allowed?
I would consider that, in many cases, helping students "outside of work time" is often still under the normal job description of a teaching assistant. However, since you are asking the question, I am going to assume that your TA went above and beyond their normal calling. Even in that case, a gift might make someone uncomfortable, and should certainly never be given before grades are final.
Other options that might achieve what you want without making anyone uncomfortable and that might have greater impact with no more effort from you than a gift could include:
Nomination for a teaching award. Many institutions have some formal mechanism for recognizing great teaching assistants; even a nomination could be something that TA could use in the future on a job application.
Letting the professor know how helpful your TA was. Professors don't see all aspects of their TA's work. Knowing that a particular TA was helpful to the students might help that TA get a future position, or is material that can be included a letter of recommendation. You could also ask the professor if there is anyone else in the department where you can send compliments for a particular TA.
There is also nothing wrong with thanking your TA directly and making it clear their time was appreciated - no need to go overboard, just a simple email is sufficient.
I would suggest writing them a note or card letting them know how much you appreciated their help; a couple of have students have done this for me in the past, and I've been really tickled by it. It scratches the same itch as a gift, but doesn't require figuring out what they would like. I wouldn't have minded getting chocolate instead, but it would have felt a little stranger.
To expand on something Bryan Krause said above: even if there's no obvious mechanism to nominate them for an award, you can tell them if they need a letter from a student in the future (these are standard for teaching awards and tenure applications, though oddly not job applications), you would be happy to write one. That both expresses your appreciation and could be useful to them.
Do beware of regulations at your particular University - At my institution it is forbidden as a TA to receive gifts or payment for TA work from students.
I think it is designed to stop us using our TA positions to make extra money from students for additional tuition but it covers literally anything. I had a student keep offering to buy myself and a colleague for helping him outside lab hours - took a lot of explaining that we would help but couldn't accept any form of payment.
Who are you?
If you are someone higher up in your department and a TA stayed behind after running one of your labs to help you with something then I don't think anyone would object to you buying them a drink. At least where I am staff buy drinks for their peers and subordinates all the time.
OTOH if you are a student buying stuff is likely to be awkward at best (and may sometimes be officially forbidden). There will be the lingering question of whether you are trying to bribe the staff member to give you better grades.
There is a philosophical aspect which can added, and that is related to the will and worldview of the student offering the gift. Whether a gift is appropriate can only be fully known relative to the student's will and worldview. If the student has dis-associated themselves from intervening institutional concerns (whatever they may be in terms of things such as "policy" and "superiors") and is able to act freely of themselves, then the appropriateness of a gift would seem to be based exclusively inside their own determination. And due to this being based internally, only the student will be able to answer for whether such a gift is appropriate or inappropriate.
Totally fine as long as the gift itself is appropriate. If it is something small that just says "Thanks", then I'm sure they will appreciate it. Just be careful not to give the wrong impression with a gift like chocolate. Many would consider that a romantic gift. Gift cards are good. $10 - $20 to Starbucks is my go to for this type of thing.