query plan caching (sql server)
There is an algorithm (will find it), but practically it will stay there:


while being used (Edit: See AdaTheDev's answer)
until statistics update (also implied by index rebuild)
until definition changes (including whitespace and case)
SQL instance restart
until severe memory pressure


SQL Server 2008 article