The class 2 type V CRISPR effector Cas12 is thought to have evolved from the IS200/IS605 superfamily of transposon-associated TnpB proteins1. Recent studies have identified TnpB proteins as miniature RNA-guided DNA endonucleases2,3. TnpB associates with a single, long RNA (ωRNA) and cleaves double-stranded DNA targets complementary to the ωRNA guide. However, the RNA-guided DNA cleavage mechanism of TnpB and its evolutionary relationship with Cas12 enzymes remain unknown. Here we report the cryo-electron microscopy (cryo-EM) structure of Deinococcus radiodurans ISDra2 TnpB in complex with its cognate ωRNA and target DNA. In the structure, the ωRNA adopts an unexpected architecture and forms a pseudoknot, which is conserved among all guide RNAs of Cas12 enzymes. Furthermore, the structure, along with our functional analysis, reveals how the compact TnpB recognizes the ωRNA and cleaves target DNA complementary to the guide. A structural comparison of TnpB with Cas12 enzymes suggests that CRISPR–Cas12 effectors acquired an ability to recognize the protospacer-adjacent motif-distal end of the guide RNA–target DNA heteroduplex, by either asymmetric dimer formation or diverse REC2 insertions, enabling engagement in CRISPR–Cas adaptive immunity. Collectively, our findings provide mechanistic insights into TnpB function and advance our understanding of the evolution from transposon-encoded TnpB proteins to CRISPR–Cas12 effectors.