How do ribosomes interpret stop codons as selenocysteine and pyrrolysine?
Summary
Reassignment of an UGA codon as a selenocysteine codon is thought to involve the interaction of obligatory secondary structure elements called selenocysteine insertion sequences (SECIS) with the dedicated translation elongation factor that presents a unique tRNA aminoacylated with selenocysteine to the ribosome. However the location of these elements and their specificity differ between prokaryotes and eukaryotes. Less is known about the less common reassignment of UAG to pyrrolysine, but some organisms may employ similar mechanisms to those involving selenocysteine, whereas others may almost completely avoid the use of UAG as a termination codon.
Preliminary — Presentation of selenocysteine to the ribosome
Although the question specifically relates to the translation of erstwhile termination codons, it is worth mentioning how selenocysteine (Sec) is presented to the ribosome.
     In most species a specific tRNA — tRNASec — is aminoacylated with serine catalysed the standard seryl-tRNA synthetase. The Ser-tRNASec is then converted to Sec-tRNASec — in Escherichia coli, by the product of the SelA gene.
    Whereas all other charged elongator tRNA species are brought to the A-site of the ribosome by the same elongation factor (EF-Tu in prokaryotes, EF-1 in eukaryotes), there is a specific elongation factor for binding Sec-tRNASec.
Reprogramming UGA codon as a selenocysteine codon
The reprogramming of UGA as a selenocysteine codon requires a stem–loop secondary structure — the SECIS — in both prokaryotes and eukaryotes. However there are differences in how these operate. Quoting from a Nature News and Views piece written in 2007:

In bacteria there is a site-specific recoding signal embedded in mRNA that flags a nearby UGA codon as encoding selenocysteine rather than stop (Fig.b). In other organisms, ranging from worms to mammals, a recoding signal located outside the coding part of the mRNA (Fig.c) signifies that all UGA codons in its mRNA specify selenocysteine (one essential human mRNA has ten UGAs).


The mode of action  of the SECIS elements is envisaged as interaction with the Sec-tRNASec-specific elongation factor, as in this illustration from Ch 13 of Molecular Biology by Clark et al.:

A shortcoming of this cartoon, is that it fails to show that in prokaryotes the SECIS element encompasses the UGA codon. This is important as it may indicate that  termination factors are prevented access to the UGA by secondary structure, which the Sec-specific elongation factor is able to unwind. (In eukaryotes there may UGA codons may also be made inaccessible by local secondary structure.)

However sequence context effects have also been suggested.
Pyrrolysine
The situation for pyrrolysine (see, for example, Ambrogelly et al. (2007)) differs from that for selenocysteine in that it is synthesised before it is charged to a specific tRNAPyl in a reaction catalysed by a specific pyrrolysyl-tRNA synthetase. The reassignment of UAG to pyrrolysine was thought to involve a change (or almost complete change) in the genetic code of certain archaea in which it is found, with UAG not or rarely being used as a termination codon. However, according to the Nature News and Views article aleady cited, this is not the case for other organisms, in which I am unaware of the mechanism of reassignment.