I think you've hit the nail on the head with this question: the composition, tactics and effectiveness of the Roman army/armies fluctuated greatly over time (especially if we consider the Byzantines ‘Roman’ too), and a very important factor in that fluctuation was the relative strength and capabilities of their contemporary enemies. The Cambridge History of Greek and Roman Warfare sums up the army’s success in its period of greatest dominance, the Principate, as a product of good leadership (professional centurions as well as decent officers), the professionalism and tactical expertise of the army, superior training and superior logistics (155-6). But the weakness of their enemies was a huge factor.
The nigh-invincibility of the Roman military in the Principate,* which covers a good couple of centuries of warfare, is prominently attributed to the weakness and relative disorganisation of Rome’s external enemies at this time (Gillver from the CHGRW, 156). In fact, after the defeat of Hannibal at Zama, within a few decades Rome had crushed every major power within its possible sphere of influence. Parthia was its only remaining competitor, and those two powers were so far separated from each other’s core territories that they were never any serious strategic threat to one another. Furthermore, in the Late Republic and Early Empire the Parthians only waned in power while the Romans waxed, and the Iranians offered less and less competition even on their own borders until they were re-invigorated under the Sassanids in AD 224. While the Sassanids were a different kettle of fish entirely, the Parthians had always lacked the logistical, professional and technical strength of the Roman army (Kennedy 86-8). Their threat to the Roman army as a whole was limited to tactical victories close to their borders, as major as they could sometimes be (eg. Carrhae). On the whole the Parthians were no real threat to Rome, but Rome was a huge threat to their western provinces. So essentially, with the fall of Carthage and the Hellenistic monarchs Rome had no serious military competition until the rise of Germanic confederations and the Sassanids in the 2-3rd centuries AD.
The Roman army as you might expect did its most impressive work in the Mid Republic, when it conquered Italy, Sicily, Sardinia and Corsica, Spain, North Africa, Greece, Macedon, Egypt among other places. This period (roughly the 4th to 2nd century BC) is also when Rome defeated its deadliest enemies: the Samnites, Pyrhhus, Carthage, Macedon, the Seleucids. Of course, Carthage was the greatest threat. Due to issues of leadership and the limitations of Rome’s militia system, wherein a newly recruited legion might have very few veterans and little training that meant it took years for an army to become really effective, Rome’s army had some mixed successes against Hannibal but remained pretty successful against other Carthaginian commanders: generalship was always a decisive factor (Sabin from CHGRW 430, 433). However, the material resources of Rome (especially manpower) were decisive in saving them from Hannibal (and to a lesser extent Pyrrhus), who killed well over 100,000 Roman and Italian soldiers in just over two years.
The Roman army professionalised over the remaining two centuries of the Republic and was in general a more effective beast in the 1st century BC. By this time Rome spent the vast majority of time fighting itself, while the Mithradatic Wars, Gallic Wars, and Marius’ campaigns against the Germans showed what even a fraction of the Roman army could do when well-led against even the most powerful contemporary enemies. The professionalization of the army in the Late Republic and Principate combined with the consolidation and organisation of the Roman state’s enormous resources in support of the army, in my mind, means that the army was most effective in this period. It also so happens that in getting to this point, the Romans had destroyed all other significant military powers around the Mediterranean. So while the the Roman army at its most effective in the Principate, it was also unchallenged by any comparable power.
I’m not an expert on the later Roman army, but it is my understanding the Roman army remained generally effective, but factors such as increasingly prevalent civil conflict and political instability, the increasing number, strength and proximity of Rome foreign enemies (esp. the Sassanids, German confederations and the migratory steppe peoples) and the structural decline of the Roman state (for instance making it harder to pay for the army or recruit good troops) certainly contributed to the decreased relative effectiveness of the Roman army as the Empire aged. Even so, it could be said that the Roman army remained totally dominant in pitched battle until the late 4th century AD (Austin & Rankov 243). The Roman army remained large, powerful and highly technically sophisticated even in this late period and in some ways, such as intelligence-gathering, actually got better (246). I’m sure there is plenty of room for debate on this, but beside structural and political upheavals from the 3rd century AD onward that severely hindered the army’s ability to oppose powerful external enemies, the increasing relative power of Rome’s external enemies from around the same time was certainly a major factor in the (very) gradual disappearance of the Roman army’s complete battlefield superiority.
Sources:
Austin, NJE and Rankov, NB. Exploratio: Military and political intelligence in the Roman world from the Second Punic War to the battle of Adrianople, London: Routledge 1995.
Gilliver, K. “Land Battle” in the Principate, from the Cambridge History of Greek and Roman Warfare, 2007, 122-157.
Kennedy, David, "Parthia and Rome: eastern perspectives", from The Roman Army in the East, ed. David L. Kennedy, Journal of Roman Archaeology, Supplementary Series 18, Curshing-Malloy Inc., Ann Arbor, 1996, 67-90.
Sabin, P. “Battle” in the Hellenistic/Republican Era, from the Cambridge History of Greek and Roman Warfare, 2007, 399-433.