There are two types of Ismahs. The first type is the major, divine Ismah—this is not what is meant by the hadith. Such Ismah is not something we can really achieve.
The second type is the relative Ismah, meaning we avoid deliberately committing sins. We might forget or make a mistake, but we don’t deliberately sin and we try to dedicate our actions to Allah. If we do this for 40 days then we get that effect.