A homemade syrup will spoil eventually, and how long that takes depends principally on how much sugar is in it.
Why sugar preserves
A high sugar concentration draws water out of microbial cells, which makes it difficult for yeasts and bacteria to survive. It is the same principle behind jam.
The effect scales with concentration, which is why a two to one syrup lasts considerably longer than a one to one.
Below a certain concentration the effect largely disappears, which is why a dilute syrup or one with a great deal of fruit juice in it behaves more like a perishable food.
Rough shelf lives
These assume refrigeration in a clean sealed bottle.
One to one simple syrup: around a month.
Two to one rich syrup: several months, and often longer.
Syrups with dried spices: around a month.
Syrups with fresh herbs, ginger or citrus: one to two weeks.
Syrups containing fruit purée or juice: a week at most.
Honey and agave syrups: a few weeks, since both are diluted from a high-sugar base.
Anything containing dairy: treat as you would the dairy itself.
Extending it
Three approaches, in order of usefulness.
Make it richer. Moving from one to one to two to one multiplies the shelf life substantially and adds body at the same time.
Add a small measure of a neutral spirit, around 20 to 30 millilitres of vodka per litre of syrup. It raises the alcohol content enough to inhibit growth without being detectable in a drink.
Freeze it. Portioned into an ice cube tray and transferred to a bag, syrup keeps for months and a cube thaws in a glass within a minute. This is the best answer for seasonal ingredients.
Hygiene
The main determinant beyond concentration.
Use a clean bottle, rinsed with boiling water and dried completely.
Fill close to the neck, since air in the bottle carries organisms and accelerates spoilage.
Never dip a used spoon or a finger into a syrup, and pour rather than scooping.
Strain the solids out. Fruit, herbs and spices left in the bottle spoil considerably faster than the syrup around them.
How to tell it has gone
Look first. A syrup that has turned cloudy when it was clear, or developed a film or visible growth, should be discarded.
Then smell. Fermentation produces a distinctly yeasty, alcoholic or vinegary smell.
Bubbles rising in a still bottle, or pressure when the cap is opened, indicate active fermentation.
A sour or off taste is the last check, and by that point the earlier signs will usually have been visible.
When in doubt, discard it. A batch of syrup costs pennies and is not worth any uncertainty.
What fermentation means
Wild yeasts consuming the sugar and producing alcohol and carbon dioxide.
It is not dangerous in the sense of producing anything toxic, and it does mean the syrup is no longer what it was and is spoiling.
A fermenting syrup in a tightly sealed bottle can build pressure, so open anything suspect carefully and over a sink.
Practical routine
Make small quantities and make them often, rather than a large batch that has to last.
Label every bottle with the contents and the date, without exception. An unlabelled bottle of amber liquid in a fridge is either thrown away or used without knowing what it is.
Keep a rich plain syrup as the workhorse, since it lasts longest, and make flavoured syrups in the quantity you will actually use within a fortnight.
And check the fridge shelf occasionally rather than discovering something from March in July.
Room temperature storage
A very rich syrup, at two parts sugar or above, is reasonably stable at room temperature for a period, which is how commercial syrups are sold.
Homemade ones lack the additional preservatives and the sterile bottling, so refrigeration remains the sensible default.
The practical summary
Make it rich, keep it cold, fill the bottle, label it, and use it within a month.
Anything beyond that is managing a problem you could have avoided by making less.