Essentially you're asking how to identify a 74 series logic IC.
It's really tricky.
In most cases the power supply pins are in predictable places, but do you wish to allow for the IC being inserted incorrectly?
Also how do you intend to detect how many pins the device has?
The easiest way to go about this (once you've determined the power supply pins) is to apply a weak pull up on each pin. This is safe because the resistance is high, and it can be really weak because TTL will float high if left disconnected.
If any pin is still low, it's clearly an output.
You can then go from pin to pin pulling each pin (weakly) low. If it stays high, its an output. If any other pin changes state then it's clearly an input and those other pins are clearly outputs.
You may have difficulty with pins that can be either inputs or outputs, or which are tri-state.
Once you've determined which pins are definitely inputs and outputs (it will be a subset of the pins) then you can compare this to all known 74 series ICs, dismissing any that are obviously incorrect.
Then it is a case of trying to determine from the list of what remains, which chip you have.
Then, presumably, you go ahead and test the functionality completely.
It's a non-trivial project.