You can safely keep cooked pasta in the refrigerator for 4 to 5 days before it goes bad.
After 5 days the cooked pasta may develop some mold and bacteria and should probably be thrown out especially if you see any visible mold or it smells bad or tastes bad.
If you want to keep the cooked pasta longer than 4 days you should freeze it instead.
When frozen the cooked pasta will keep for 6 months and even be safe as long as a year.
But the cooked pasta won't be as fresh or good tasting after a few months so it's best to eat the cooked pasta within a few months of freezing it.
After cooking the pasta the cooked pasta can remain out of the refrigerator at room temperature for up to 2 hours.
After 2 hours at room temperature the cooked pasta can grow bacteria rapidly and should not be eaten.
But as long as you refrigerate any leftover cooked pasta within 2 hours then it will be fine.
To keep the cooked pasta longer then it's recommended to freeze it instead.