1- As for hadiths that state an illegitimate person should not be a judge or prayer leader, the aim is to keep society healthy and not normalize adultery. Imagine if there is an important prayer leader and he is illegitimate. It looks bad for the community. People will think “Oh, I guess adultery is not that bad after all…see this leader he is illegitimate. No big deal.” This normalizes it. So to discourage people from committing adultery, Islam prohibits illegitimate children from having such positions. It’s for the overall good of society.
As for the testimony, again it goes back to the issue of normalizing it in society. Islam doesn’t want such people to have an important public position because:
A- It makes adultery normal in society. People will see witnesses in court who are illegitimate and so over time it becomes normal.
B- Islam wants to put pressure on potential adulterous parents: don’t have an illegitimate child because if you do, then you will deny him many privileges. So be wise and don’t
C- Many illegitimate children grow up in bad and corrupt environments, and so it’s safer not to have them assume a very critical role such as witnessing in court
By the way, all this applies to someone who is known to be an illegitimate child. We don’t have to go and investigate such matters.
2- As for an illegitimate child not being eligible to go to heaven, there are some hadiths to that effect, but scholars have different perspectives on them. One perspective is that these narrations are talking about the fate of many of them, not all of them. Naturally, an illegitimate child usually grows up in a corrupt-prone environment—decreasing the chances of him being guided. Just like kids who grow up in bad neighborhoods around gangs. They tend to be aggressive, violent, and criminals. But if the illegitimate child does good and believes in the truth, he can go to heaven. Here’s one hadith in Kafi:
عن ابن أبي يعفور عن الصادق (عليه السلام) قال: ”إن ولد الزنا يُستعمل، إن عمل خيراً جُزِيَ به، وإن عمل شراً جُزِيَ به“. (الكافي ج8 ص238).
Basically the Imam says that if the illegitimate child does good, he will be rewarded for it.
So these hadiths that state they don’t enter heaven are basically warning the parents to not put them in a corrupt environment.