In raw edible cookie dough it is almost entirely ingredient dependant, then of course any contact points for contamination. Standard “raw” flour contains e-coli and other bacterium, that without a kill step can be harmful to health. Specialty heat treated flours are used explicitly for edible cookie dough. Eliminate butter and use vegetable shortening and a non-dairy natural butter flavor. Eliminate milk, and use high heat treated milk powder instead. From there it is of course how sterile you processing is. Best case scenario is to test samples for micro biological activity at intervals over, and beyond, the targeted shelf life.
In baked cookies it is slightly different, in that we have a solid kill step, and now the two items of focus are water activity and softness, or also staling. On average most cookie result in around 0.6 aW. Anything below 0.65 aW is a good target that will limit possibilities of bacterial growth. To achieve this and have the cookies stay soft is a lot to do with managing humectants. Sugars come in many different forms, and corn syrup is used for its humectant properties, low aW and functionality to not crystallize like sucrose. Brown sugar is helpful here too, as the brown sugar syrup contains fructose that does not recrystallize. Similarly molasses. Adding phosphates or acid to inver some of the sugar during baking can also be helpful.
To reduce staling I would first look to fat amount in the formula and emulsifiers. Other things such as gums may be of help as well.
If there are other specifics about shelf life you are targeting, it would be helpful to have more details to go on.