In order for the government to have power to regulate any kind of behavior, it must have "jurisdiction." In most cases, jurisdictional issues are straightforward—the crime occurs within a particular state's boundaries, violating that state's laws, and therefore giving that state the power to prosecute. Likewise, a crime that occurs on federal property—for example, Yellowstone National Park—authorizes the feds to deal with the suspect. But some crimes violate both state and federal law, enabling both governments to bring criminal charges. Some crimes, on the other hand, are left only to the states.
As long as there is a sufficient federal "nexus," the U.S. government can regulate almost any kind of behavior. That means, for example, that it's possible to violate federal law through conduct that occurs solely on one state's land. For instance, the federal government has authority under the U.S. Constitution to regulate interstate commerce. Crimes with sufficient connection to interstate commerce—for example, transporting a stolen vehicle between states—are properly federal offenses. (United States v. Peters, 952 F.2d 960 (7th Cir. 1992).) Oftentimes, the connection to interstate commerce is rather slight.
Example: Eight men try to rob a racetrack in Illinois but are thwarted by security guards. In their subsequent prosecution for attempted robbery, they argue that the federal government doesn't have jurisdiction to prosecute them because the alleged crime occurred on state grounds. But because the racetrack's money is federally insured, many of the horses' owners come from out of state, and the track uses several out-of-state services (for betting machines and advertising materials, for example), jurisdiction was properly vested in the U.S. government. (See United States v. Harty, 930 F.2d 1257 (7th Cir. 1991).)
As noted above, some crimes fall under both state and federal jurisdiction. In this case, who takes the case? In many instances, one government with jurisdiction will defer to another. The deferring government will step in only if the other prosecution fails. That said, sometimes both governments will pursue criminal charges.
In some sense, it seems unfair for a defendant to undergo criminal prosecution more than once for the same alleged offense. After all, doesn't the prohibition against double jeopardy prevent exactly that? Unfortunately for some defendants, the answer is no. The Constitution's Double Jeopardy Clause prevents multiple prosecutions or punishments by the same "sovereign." Successive state and federal prosecutions don't violate the clause because state and federal governments are separate sovereigns.
Example: In 1991 in Los Angeles, several police officers were involved in the vicious beating of motorist Rodney King. Because of the case's tremendous publicity, the court changed venue to Ventura County in Southern California. The Ventura jury acquitted the officers on all but one charge (as to which it hung), sparking the 1992 Los Angeles Riots. Months later, a federal grand jury indicted the officers for the same beating under a law punishing anyone who, acting under governmental authority, violates another person's federal rights. (18 U.S.C. § 242.) A federal jury ultimately convicted two of the four officers under the U.S. law, which prosecutors frequently invoke in excessive force cases.
Example: Federal authorities charged NFL quarterback Michael Vick for running an interstate dogfighting business that involved dog executions and gambling, among other illegalities. A federal court sentenced him to 23 months in prison. The state of Virginia separately prosecuted Vick for the dogfighting ring. After his federal conviction, while he was still in U.S. custody, he pleaded guilty to a state charge. (The sentence for the state crime was essentially folded into the federal sentence he was serving.)
If you're facing criminal charges, talk to a criminal defense lawyer. Some lawyers practice in both state and federal courts, while others might practice in only one or the other.