It depends on the circumstances.
If you thought of a particular invention, say five years ago, but never built or sold the invention, and someone else independently thought of the same invention and filed a patent application for the invention, you are out of luck. You lost “the race to the patent office”.
The U.S., and most of the rest of the world, patent rights are granted to the first person to file a patent application for a given invention.
The situation is not quite so clear if the person who filed the application got the idea for the patented invention from you in the first place. The Patent Act provides …
Whoever invents or discovers any new and useful process, machine, manufacture, or composition of matter, or any new and useful improvement thereof, may obtain a patent therefor (35 U.S.C § 101)
So the filer did not technically “invent or discover” the patented invention. You might say they “stole” the idea,
If you can get the patent owner to voluntarily change the inventorship on the patent and compensate you accordingly (assuming they made any money on the patent), then it's pretty simple procedure.
If the patent owner won't cooperate, you can bring a Federal lawsuit under 35 U.S. Code § 256 to correct inventorship on the patent and claim whatever monetary damages you think you can prove. You will need to prove by the preponderance of the evidence you are the actual inventor and that the patent owner (or whoever originally filed the application) got the idea for the invention described in the patent from you. Bear in mind this may or may not be easy to do, and Federal lawsuits can be expensive.
The best way to avoid having someone patent your invention is as follows …
(1.) If you think an idea you have for an invention might be valuable, keep it to yourself. Trust no one!
(2.) If you tell anyone else your ideas, have them sign a non-disclosure agreement with you before you tell it to them.
(3) File a provisional patent application on the idea, followed by a full-nonprovisional patent application if you can justify the cost.
If you have any questions, feel free to contact me.