Director Rob Marshall, who worked with the actress in the musical Nine, asked Dench to participate in the sequel with a small role. Here's a description of the scene she'll be in below, where she'll appear along with her Chocolat co-star, Johnny Depp:
"On Stranger Tides, which stars Depp, Penélope Cruz (also a Nine alumna) and Geoffrey Rush, starts with a chase sequence - described as a bit like Charlie Chaplin getting away from the Keystone Kops - involving Depp's character Captain Jack Sparrow. Cap'n Jack escapes into a carriage belonging to a noblewoman, played by Judi. As he ravishes her, he manages to rob her."
Judi Dench's cameo will only equate to a minute or two of screen time.