For instance, Cardano published the formula for the solution to the cubic after promising Tartaglia he would not, and yet in textbooks the formula is often named after Cardano. What is the reason for this?
The case of Cardano is clear: unlike Tartaglia, he published the result. By modern standards a result has to be published for any claim of priority.
People read books and papers and refer on them. Normally mathematicians (and others) are not very much concerned with the question "who really did this first". One reason is that this question rarely has a simple answer, because most discoveries evolve gradually. (A had the idea but his proof had gaps, B had a correct proof, C generalized this, D shows how important it is, sometimes D read A,B,C, sometimes discovers this independently, and so on. A may be an obscure person, nobody reads his writings, and C may be famous).
In general, the result does not have to be "stolen". In many cases A and B discover something independently, say A was earlier, but the publication of B attracted larger attention for some reasons, and the result is named after B. In other cases, B really gets the idea from A, develops it further, or exposes better, and does not mention A. Before the 20th century mathematical papers had no reference lists, and many people did not consider it necessary to give accurate credits.
For most mathematicians, the names of the theorems are just the labels which permit to recognize them easily. They are not concerned about fair credit. They leave this to historians, and continue to use the recognized names of the well-known results. There are thousands of such cases besides Cardano, and I do not want to make a list. I mean something called "Theorem of X", while everyone knows that X was not the first, or even has nothing to do with this theorem.
Arnold once stated "Arnold's Principle": If something is named after X, this indicates that X had nothing to do with this. (Someone added: "Arnold's Principle applies to itself as well" :-) )
EDIT. In most cases it is impossible to answer the question "who was the first to...", so this question so frequently asked on this site has no meaning.